繁体   English   中英

为多个UIAlertController调度asyncAfter

[英]Dispatch asyncAfter for multiple UIAlertControllers

我有一个调度异步,我希望在屏幕上弹出4条警报。然后在显示新警报之前将每条警报关闭。 (我在“提醒”之间设置了3秒的延迟)

class ViewController: UIViewController {

    var counter = 1

    override func viewDidLoad() {
        super.viewDidLoad()

        for _ in 1...4{
            DispatchQueue.main.asyncAfter(deadline: .now() + 3.0 * Double(counter)  , execute: {
                print("called")
                self.showAlert()
            })
        }
    }

    func showAlert(){

        let alert = UIAlertController(title: "sampleTitle \(counter)", message: "sampleMessage \(counter)", preferredStyle: .alert)
        let action = UIAlertAction(title: "OK", style: .default, handler: nil)
        alert.addAction(action)

        counter += 1
        if self.presentedViewController != nil {
            dismiss(animated: true, completion: nil)
            UIApplication.topViewController()?.present(alert, animated: true, completion: nil)
        }else{
            UIApplication.topViewController()?.present(alert, animated: true, completion: nil)
        }
    }
}

问题1:但是由于某种原因,整个for loop被执行而之间没有任何延迟。 我猜我对主队列是串行队列不了解。

问题2:即使我解散了presentedViewController,我也从控制台获得了以下日志。

called
called
called
called
2017-06-26 11:10:57.000 topViewAndAlertTest[3360:210226] Warning: Attempt to dismiss from view controller <topViewAndAlertTest.ViewController: 0x7fe630c03350> while a presentation or dismiss is in progress!
2017-06-26 11:10:57.001 topViewAndAlertTest[3360:210226] Warning: Attempt to present <UIAlertController: 0x7fe630c04180> on <UIAlertController: 0x7fe630f06fe0> while a presentation is in progress!
2017-06-26 11:10:57.001 topViewAndAlertTest[3360:210226] Warning: Attempt to dismiss from view controller <topViewAndAlertTest.ViewController: 0x7fe630c03350> while a presentation or dismiss is in progress!
2017-06-26 11:10:57.001 topViewAndAlertTest[3360:210226] Warning: Attempt to present <UIAlertController: 0x7fe630c06b40> on <UIAlertController: 0x7fe630f06fe0> while a presentation is in progress!

仅供参考,我的topviewcontroller正在使用此答案中的代码

问题3:仅弹出2条警报...我从没看到第三,第四警报!


编辑:

经过rmaddy的建议,我的错误略有改变:

called
called
called
called
2017-06-26 11:59:33.417 topViewAndAlertTest[4834:441163] Warning: Attempt to dismiss from view controller <topViewAndAlertTest.ViewController: 0x7fb596d05a30> while a presentation or dismiss is in progress!
2017-06-26 11:59:33.417 topViewAndAlertTest[4834:441163] Warning: Attempt to dismiss from view controller <topViewAndAlertTest.ViewController: 0x7fb596d05a30> while a presentation or dismiss is in progress!

我少收到2条警告。 但仍然:警报1出现在屏幕上时,警报2便将其关闭,仅此而已! 没有延迟,没有第三,第四警报!

细节

xCode 8.3.2,Swift 3.1

完整代码

import UIKit

class ViewController: UIViewController {

    var counter = 1

    private var alertViewController: UIAlertController?

    override func viewDidLoad() {
        super.viewDidLoad()

        DispatchQueue.global(qos: .utility).async {
            for _ in 1...4 {
                sleep(2)
                DispatchQueue.main.async {
                    print("called")
                    self.showAlert()
                }
            }
        }
    }

    private func createAlertView() -> UIAlertController {
        let alertViewController = UIAlertController(title: "sampleTitle \(counter)", message: "sampleMessage \(counter)", preferredStyle: .alert)
        let action = UIAlertAction(title: "OK", style: .default, handler: nil)
        alertViewController.addAction(action)
        return alertViewController
    }

    func showAlert(){

        let presentAlert = {
            DispatchQueue.main.async { [weak self] in
                if let _self = self {
                    _self.alertViewController = _self.createAlertView()
                    UIApplication.topViewController()?.present(_self.alertViewController!, animated: true, completion: nil)
                }
            }
        }

        DispatchQueue.main.async { [weak self] in
            if let alertViewController = self?.alertViewController {
                alertViewController.dismiss(animated: true) {
                    presentAlert()
                }
            } else {
                presentAlert()
            }
            self?.counter += 1
        }
    }
}

extension UIApplication {
    class func topViewController(base: UIViewController? = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController) -> UIViewController? {
        if let nav = base as? UINavigationController {
            return topViewController(base: nav.visibleViewController)
        }
        if let tab = base as? UITabBarController {
            if let selected = tab.selectedViewController {
                return topViewController(base: selected)
            }
        }
        if let presented = base?.presentedViewController {
            return topViewController(base: presented)
        }
        return base
    }
}

暂无
暂无

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

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