在ViewController之外显示UIAlertController

时间:2015-05-05 11:56:50

标签: ios swift uialertcontroller

我无法显示我的UIAlertController,因为我试图在不是ViewController的类中显示它。

我已经尝试添加它了:

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

UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)

哪个不行...... 我找不到任何对我有用的解决方案。

4 个答案:

答案 0 :(得分:31)

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

现在它很简单:

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

修改

对于Xcode 8.0&斯威夫特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);
        }
    }
}

答案 1 :(得分:0)

创建一个从当前视图控制器调用的辅助函数,并将当前视图控制器作为参数传递:

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

这应该有用。

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