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

前端 未结 17 934
花落未央
花落未央 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:07
    let storyboard = UIStoryboard(name: "test", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "teststoryboard") as UIViewController
    UIApplication.shared.keyWindow?.rootViewController?.present(vc, animated: true, completion: nil)
    

    This seemed to work to make sure it's the top most view.

    I was getting an error

    Warning: Attempt to present myapp.testController: 0x7fdd01703990 on myapp.testController: 0x7fdd01703690 whose view is not in the window hierarchy!

    Hope this helps others with swift 3

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

    Swift 5.1:

    let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)
    let mainViewController = storyboard.instantiateViewController(withIdentifier: "ID")
    let appDeleg = UIApplication.shared.delegate as! AppDelegate
    let root = appDeleg.window?.rootViewController as! UINavigationController
    root.pushViewController(mainViewController, animated: true)
    
    0 讨论(0)
  • 2020-11-30 22:11

    Swift 4

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

    Swift 3.

    Call this function to get the topmost view controller, then have that view controller present.

    func topMostController() -> UIViewController {
        var topController: UIViewController = UIApplication.shared.keyWindow!.rootViewController!
            while (topController.presentedViewController != nil) {
                topController = topController.presentedViewController!
            }
            return topController
        }
    

    Usage:

    let topVC = topMostController()
    let vcToPresent = self.storyboard!.instantiateViewController(withIdentifier: "YourVCStoryboardID") as! YourViewController
    topVC.present(vcToPresent, animated: true, completion: nil)
    
    0 讨论(0)
  • 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) 
    }
    
    0 讨论(0)
  • 2020-11-30 22:12

    Use of main thread to present and dismiss view controller worked for me.

    DispatchQueue.main.async { self.present(viewController, animated: true, completion: nil) }
    
    0 讨论(0)
提交回复
热议问题