ios - Xcode, swift : alert not dismissing

标签 ios swift xcode

class AVC: UIViewController {
    override func viewDidLoad(){
        let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)

        let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
        loadingIndicator.startAnimating();

        alert.view.addSubview(loadingIndicator)
        viewController.present(alert, animated: true, completion: nil)
        self.a()
    }

    func a(){
        ZZZ.remove(for: self)
    }
}

class ZZZ {

    func remove(for viewController: UIViewController){
        viewController.dismiss(animated: false, completion: nil)
        //Why won't the alert created in AVC be dismissed from here?
    }
}

你好,
我有一个 View Controller AVC,它创建一个警报和一个加载指示器。
我希望使用 ZZZ 的删除功能从另一个名为 ZZZ 的 swift 文件中消除此警报。
然而,即使我的代码明确要求,ZZZ 也不会消除警报。
我该如何解决这个问题?
谢谢

最佳答案

这里共享的是用于显示警报的自定义类

class Shared: NSObject {
    func waitAlert() -> UIAlertController {
        let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
        alert.view.tintColor = UIColor.black
        let loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50)) as UIActivityIndicatorView
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
        loadingIndicator.startAnimating();
        alert.view.addSubview(loadingIndicator)
        return alert
    }
}

因此,在您的 View Controller 中,您可以显示警报

let alert = Shared().waitAlert()
present(alert, animated: true, completion: {
   alert.dismiss(animated: true, completion: {
    //do your code
   })
})

关于ios - Xcode, swift : alert not dismissing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51820664/

相关文章:

iphone - 从其类中引用实体

ios - 如何在 `UIImageView` 中的自定义单元格中更改 `UITableView`

ios - 为什么我的 TableView 阴影会随着我的 TableView 一起滚动?

swift - 为什么我不能用 (&myUInt32) 或 (&[myUInt32]) 初始化 Swift UnsafeMutablePointer<UInt32>

ios - 访问 UITableView Cell 标签文本并在方法中使用它

ios - 将 CURL 转换为 Swift

php - 在后台 iOS GCM 中未收到推送通知

iOS 11.2.5 - didRegisterForRemoteNotificationWithDeviceToken - 无响应

ios - 如何动态改变iOS应用程序中的标签数量?

swift - Swift 中关联类型枚举的默认值