Show two ViewController from AppDelegate

扶醉桌前 提交于 2019-12-11 05:56:23

问题


When APP is Launching - start SigninView - it's Okey. Next if success - I need showTripController(). Function work but nothing show? What's a problem?

func showSigninView() {
    let controller = self.window?.rootViewController!.storyboard?.instantiateViewControllerWithIdentifier("DRVAuthorizationViewController")
    self.window?.rootViewController!.presentViewController(controller!, animated: true, completion: nil)
}

func showTripController() {
    let cv = self.window?.rootViewController!.storyboard?.instantiateViewControllerWithIdentifier("DRVTripTableViewController")
    let nc = UINavigationController()
    self.window?.rootViewController!.presentViewController(nc, animated:true, completion: nil)
    nc.pushViewController(cv!, animated: true);
}

回答1:


First of all you must add this before you use window :

self.window.makeKeyAndVisible()

Another thing to keep in mind is:

Sometimes keyWindow may have been replaced by window with nil rootViewController (showing UIAlertViews, UIActionSheets on iPhone, etc), in that case you should use UIView's window property.

So, instead of using rootViewController, use the top one presented by it:

extension UIApplication {
    class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {
        if let nav = base as? UINavigationController {
            return topViewController(base: nav.visibleViewController)
        }
        if let tab = base as? UITabBarController {
            if let selected = tab.selectedViewController {
                return topViewController(base: selected)
            }
        }
        if let presented = base?.presentedViewController {
            return topViewController(base: presented)
        }
        return base
    }
}

if let topController = UIApplication.topViewController() {
    topController.presentViewController(vc, animated: true, completion: nil)
}



回答2:


Replace last 3 lines of showTripController as below:

let nc = UINavigationController(rootViewController: cv));
self.window!.rootViewController = nc


来源:https://stackoverflow.com/questions/36895419/show-two-viewcontroller-from-appdelegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!