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

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

    You just need to perform a selector with a delay - (0 seconds works).

    override func viewDidLoad() {
        super.viewDidLoad()
        perform(#selector(presentExampleController), with: nil, afterDelay: 0)
    }
    
    @objc private func presentExampleController() {
        let exampleStoryboard = UIStoryboard(named: "example", bundle: nil)
        let exampleVC = storyboard.instantiateViewController(withIdentifier: "ExampleVC") as! ExampleVC
        present(exampleVC, animated: true) 
    }
    

提交回复
热议问题