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

前端 未结 17 1000
花落未央
花落未央 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.

提交回复
热议问题