Attempt to present UIViewController on UIViewController whose view is not in the window hierarchy

后端 未结 30 3177
一整个雨季
一整个雨季 2020-11-21 23:39

Just started using Xcode 4.5 and I got this error in the console:

Warning: Attempt to present < finishViewController: 0x1e56e0a0 > on < ViewCont

30条回答
  •  死守一世寂寞
    2020-11-22 00:07

    I've ended up with such a code that finally works to me (Swift), considering you want to display some viewController from virtually anywhere. This code will obviously crash when there is no rootViewController available, that's the open ending. It also does not include usually required switch to UI thread using

    dispatch_sync(dispatch_get_main_queue(), {
        guard !NSBundle.mainBundle().bundlePath.hasSuffix(".appex") else {
           return; // skip operation when embedded to App Extension
        }
    
        if let delegate = UIApplication.sharedApplication().delegate {
            delegate.window!!.rootViewController?.presentViewController(viewController, animated: true, completion: { () -> Void in
                // optional completion code
            })
        }
    }
    

提交回复
热议问题