本文介绍了从AppDelegate显示两个ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

启动APP时-启动SigninView-它是Okey.接下来,如果成功-我需要showTripController().功能正常,但无显示?有什么问题吗?

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);
}

推荐答案

首先,您必须在使用window之前添加它:

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

self.window.makeKeyAndVisible()

要记住的另一件事是:

有时keyWindow可能已被零rootViewController替换为窗口(在iPhone上显示UIAlertViews,UIActionSheets等),在这种情况下,您应该使用UIView的window属性.

因此,不要使用rootViewController,而要使用它提供的最上面的一个:

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)
}

这篇关于从AppDelegate显示两个ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 16:26