在ViewController之外显示UIAlertController

Mic*_*ael 15 ios swift uialertcontroller

我很难显示我的UIAlertController,因为我试图在一个不是ViewController的类中显示它.

我已经尝试过添加它了:

 var alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)

UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

这是行不通的......我找不到任何对我有用的解决方案.

Avi*_*oss 32

我写了这个extension过度UIAlertController带回show().
它使用递归来查找当前的顶视图控制器:

extension UIAlertController {

    func show() {
        present(animated: true, completion: nil)
    }

    func present(#animated: Bool, completion: (() -> Void)?) {
        if let rootVC = UIApplication.sharedApplication().keyWindow?.rootViewController {
            presentFromController(rootVC, animated: animated, completion: completion)
        }
    }

    private func presentFromController(controller: UIViewController, animated: Bool, completion: (() -> Void)?) {
        if let navVC = controller as? UINavigationController,
            let visibleVC = navVC.visibleViewController {
                presentFromController(visibleVC, animated: animated, completion: completion)
        } else
        if let tabVC = controller as? UITabBarController,
            let selectedVC = tabVC.selectedViewController {
                presentFromController(selectedVC, animated: animated, completion: completion)
        } else {
            controller.presentViewController(self, animated: animated, completion: completion);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在它很简单:

var alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
alertController.show()
Run Code Online (Sandbox Code Playgroud)

编辑:

对于Xcode 8.0和Swift 3:

extension UIAlertController {

    func show() {
        present(animated: true, completion: nil)
    }

    func present(animated: Bool, completion: (() -> Void)?) {
        if let rootVC = UIApplication.shared.keyWindow?.rootViewController {
            presentFromController(controller: rootVC, animated: animated, completion: completion)
        }
    }

    private func presentFromController(controller: UIViewController, animated: Bool, completion: (() -> Void)?) {
        if let navVC = controller as? UINavigationController,
            let visibleVC = navVC.visibleViewController {
            presentFromController(controller: visibleVC, animated: animated, completion: completion)
        } else
            if let tabVC = controller as? UITabBarController,
                let selectedVC = tabVC.selectedViewController {
                presentFromController(controller: selectedVC, animated: animated, completion: completion)
            } else {
                controller.present(self, animated: animated, completion: completion);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这在关键视图控制器已经以模态呈现某些内容的情况下不起作用。我不得不添加另一个检查`if let present = controller.presentedViewController {presentFromController(controller:presented,animated:animated,completion:completion)}else{controller.present(self,animated:animated,completion:completion)}` (2认同)