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

前端 未结 17 936
花落未央
花落未央 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:23

    Swift 3

    I had this keep coming up as a newbie and found that present loads modal views that can be dismissed but switching to root controller is best if you don't need to show a modal.

    I was using this

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc  = storyboard?.instantiateViewController(withIdentifier: "MainAppStoryboard") as! TabbarController
    present(vc, animated: false, completion: nil)
    

    Using this instead with my tabController:

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let view = storyboard.instantiateViewController(withIdentifier: "MainAppStoryboard") as UIViewController
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    //show window
    appDelegate.window?.rootViewController = view
    

    Just adjust to a view controller if you need to switch between multiple storyboard screens.

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

    For swift 3.0 and above

    public static func getTopViewController() -> UIViewController?{
    if var topController = UIApplication.shared.keyWindow?.rootViewController
    {
      while (topController.presentedViewController != nil)
      {
        topController = topController.presentedViewController!
      }
      return topController
    }
    return nil}
    
    0 讨论(0)
  • 2020-11-30 22:24

    Swift Method, and supply a demo.

    func topMostController() -> UIViewController {
        var topController: UIViewController = UIApplication.sharedApplication().keyWindow!.rootViewController!
        while (topController.presentedViewController != nil) {
            topController = topController.presentedViewController!
        }
        return topController
    }
    
    func demo() {
        let vc = ViewController()
        let nav = UINavigationController.init(rootViewController: vc)
        topMostController().present(nav, animated: true, completion: nil)
    }
    
    0 讨论(0)
  • 2020-11-30 22:26

    At this point in your code the view controller's view has only been created but not added to any view hierarchy. If you want to present from that view controller as soon as possible you should do it in viewDidAppear to be safest.

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

    I was getting this error while was presenting controller after the user opens the deeplink. I know this isn't the best solution, but if you are in short time frame here is a quick fix - just wrap your code in asyncAfter:

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.7, execute: { [weak self] in
                                    navigationController.present(signInCoordinator.baseController, animated: animated, completion: completion)
                                })
    

    It will give time for your presenting controller to call viewDidAppear.

    0 讨论(0)
提交回复
热议问题