在ViewController之外显示UIAlertController

我有麻烦显示我的UIAlertController,因为我试图显示它不是一个ViewController类。

我已经尝试添加它:

var alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert) UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil) 

哪个不工作…我没有find任何解决scheme,为我工作呢。

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

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

现在就像以下这样简单:

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

编辑:

对于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); } } } 

创build一个从当前视图控制器调用的帮助器函数,并将当前视图控制器作为parameter passing:

 func showAlertInVC( viewController: UIViewController, title: String, message: String) { //Code to create an alert controller and display it in viewController } 

如果你的解决scheme不起作用,那可能是因为那个时候没有窗口。 当我尝试在application:DidFinishLoadingWithOptions显示警报视图时,我遇到了同样的问题application:DidFinishLoadingWithOptions方法。 在这种情况下,我的解决scheme是检查根视图控制器是否可用,如果不是,则添加通知UIApplicationDidBecomeActiveNotification

 NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationDidBecomeActiveNotification, object: nil, queue: NSOperationQueue.mainQueue()) { (_) in //show your alert by using root view controller //remove self from observing } } 

这应该工作。

  UIApplication.sharedApplication().windows[0].rootViewController?.presentViewController(...)