为多个UIAlertControllers调度asyncAfter

时间:2017-06-26 15:34:29

标签: ios swift grand-central-dispatch uialertcontroller dispatch-async

我有一个调度异步,我希望在屏幕上弹出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:我在控制台中也得到以下日志,即使我正在解雇presentViewController。

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正在使用此answer

中的代码

问题3:只有2个提醒弹出...我从未看到第3次,第4次提醒!

修改

在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就会解除警报,就是这样!没有延迟没有第3,第4警报!

1 个答案:

答案 0 :(得分: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
    }
}