简体   繁体   English

Xcode,快速:警报无法消除

[英]Xcode, swift: alert not dismissing

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?
    }
}

Hi there, 嗨,您好,
I have a view controller AVC that creates an alert & a loading indicator. 我有一个视图控制器AVC,它创建警报和加载指示器。
I wish to dismiss this alert from another swift file called ZZZ using ZZZ's remove function. 我希望使用ZZZ的删除功能从另一个名为ZZZ的快速文件中消除此警报。
However, the alert is not dismissed from ZZZ even though my code clearly asks it to. 但是,即使我的代码明确要求,也不会从ZZZ中解除警报。
How do I fix this? 我该如何解决?
Thanks 谢谢

You are currently dismissing the viewcontroller, not alert. 您当前正在关闭ViewController,而不是警报。 Call 呼叫

alert.dismiss(animated: false, completion: nil)


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

Here shared is custom class for showing alert 这里共享的是自定义类,用于显示警报

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
    }
}

So in your viewcontroller you can for showing alert 因此,在您的ViewController中,您可以显示警报

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM