【问题标题】:show UIAlertController outside of ViewController在 ViewController 之外显示 UIAlertController
【发布时间】:2015-05-05 11:56:49
【问题描述】:

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

我已经尝试添加它:

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

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

哪个不工作... 我还没有找到任何适合我的解决方案。

【问题讨论】:

  • 你现在不是 UIViewController 的类部分无法测试。考虑添加一个委托或基于块的回调,以在使用此类的视图控制器上显示警报。

标签: ios swift uialertcontroller


【解决方案1】:

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

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 if let presented = controller.presentedViewController {
            presentFromController(controller: presented, animated: animated, completion: completion)
        } else {
            controller.present(self, animated: animated, completion: completion);    
        }
    }
}

现在很简单:

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

【讨论】:

  • 太棒了!非常感谢
  • 很高兴它对您有用,我一直在将它用于许多 VC 场景(标签栏控制器内导航堆栈上的模态 vc 等),请告诉我是否存在这样的情况不行……
  • SharedApplication 不能在应用扩展中使用。
  • 这在键视图控制器已经以模态方式呈现某些内容的情况下不起作用。我不得不添加另一张支票if let presented = controller.presentedViewController { presentFromController(controller: presented, animated: animated, completion: completion) } else { controller.present(self, animated: animated, completion: completion) }
【解决方案2】:

这应该可行。

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      如果您的解决方案不起作用,可能是因为当时没有窗口。当我尝试在 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
                          }
              }
      

      【讨论】:

        猜你喜欢
        • 2012-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-15
        • 1970-01-01
        • 2014-12-14
        相关资源
        最近更新 更多