Warning: Attempt to present * on * whose view is not in the window hierarchy - swift

前端 未结 17 935
花落未央
花落未央 2020-11-30 22:03

I\'m trying to present a ViewController if there is any saved data in the data model. But I get the following error:

Warning: A

相关标签:
17条回答
  • 2020-11-30 22:13

    Rather than finding top view controller, one can use

    viewController.modalPresentationStyle = UIModalPresentationStyle.currentContext
    

    Where viewController is the controller which you want to present This is useful when there are different kinds of views in hierarchy like TabBar, NavBar, though others seems to be correct but more sort of hackish

    The other presentation style can be found on apple doc

    0 讨论(0)
  • 2020-11-30 22:15

    The previous answers relate to the situation where the view controller that should present a view 1) has not been added yet to the view hierarchy, or 2) is not the top view controller.
    Another possibility is that an alert should be presented while another alert is already presented, and not yet dismissed.

    0 讨论(0)
  • 2020-11-30 22:17

    In objective c: This solved my problem when presenting viewcontroller on top of mpmovieplayer

    - (UIViewController*) topMostController
    {
        UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
        while (topController.presentedViewController) {
            topController = topController.presentedViewController;
        }
    
        return topController;
    }
    
    0 讨论(0)
  • 2020-11-30 22:18

    All implementation for topViewController here are not fully supporting cases when you have UINavigationController or UITabBarController, for those two you need a bit different handling:

    For UITabBarController and UINavigationController you need a different implementation.

    Here is code I'm using to get topMostViewController:

    protocol TopUIViewController {
        func topUIViewController() -> UIViewController?
    }
    
    extension UIWindow : TopUIViewController {
        func topUIViewController() -> UIViewController? {
            if let rootViewController = self.rootViewController {
                return self.recursiveTopUIViewController(from: rootViewController)
            }
    
            return nil
        }
    
        private func recursiveTopUIViewController(from: UIViewController?) -> UIViewController? {
            if let topVC = from?.topUIViewController() { return recursiveTopUIViewController(from: topVC) ?? from }
            return from
        }
    }
    
    extension UIViewController : TopUIViewController {
        @objc open func topUIViewController() -> UIViewController? {
            return self.presentedViewController
        }
    }
    
    extension UINavigationController {
        override open func topUIViewController() -> UIViewController? {
            return self.visibleViewController
        }
    }
    
    extension UITabBarController {
        override open func topUIViewController() -> UIViewController? {
            return self.selectedViewController ?? presentedViewController
        }
    }
    
    0 讨论(0)
  • 2020-11-30 22:20

    for SWIFT

    func topMostController() -> UIViewController {
        var topController: UIViewController = UIApplication.sharedApplication().keyWindow!.rootViewController!
        while (topController.presentedViewController != nil) {
            topController = topController.presentedViewController!
        }
        return topController
    }
    
    0 讨论(0)
  • 2020-11-30 22:20

    I have tried so many approches! the only useful thing is:

    if var topController = UIApplication.shared.keyWindow?.rootViewController
    {
      while (topController.presentedViewController != nil)
      {
        topController = topController.presentedViewController!
      }
    }
    
    0 讨论(0)
提交回复
热议问题