gpt4 book ai didi

ios - 无法从 AppDelegate 中实例化 rootViewController

转载 作者:行者123 更新时间:2023-11-28 13:05:47 26 4
gpt4 key购买 nike

我的入职流程(登录/注册)位于 UINavigationController 链之外,但是当登录通过身份验证时,我在 appdelegate 中调用一个方法,该方法应该实例化 rootViewController,然后推送给它。我试过两件事:

上面的代码片段在 appDelegate 中通过一个方法运行,该方法在响应本地通知时被调用,但在本例中不是。

  self.window?.makeKeyAndVisible()
self.window?.rootViewController?.navigationController?.popToRootViewControllerAnimated(true)

这是我尝试过的另一种方法:

 let rootViewController = self.window?.rootViewController

let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let dashboard = mainStoryboard.instantiateViewControllerWithIdentifier("DashboardViewController") as! DashboardViewController

rootViewController!.navigationController?.popToViewController(dashboard, animated: true)

这些都不起作用。我做错了什么?

最佳答案

我的登录 View Controller 也存在于我的标签栏 Controller 之外,所以这就是我所做的:

enter image description here

在 appDelegate 中:

    var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.


let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginVC = storyboard.instantiateViewControllerWithIdentifier("LoginVC") as! LoginViewController
self.window?.rootViewController = loginVC
self.window!.makeKeyAndVisible()


return true
}

然后我的 loginViewContoller.swift 中有一个方法:

    @IBAction func loginPushed(sender: AnyObject) {


//other stuff here

let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = UITabBarController()
let first = mainStoryBoard.instantiateViewControllerWithIdentifier("firstNavController") as! UINavigationController
let second = mainStoryBoard.instantiateViewControllerWithIdentifier("secondNavController") as! UINavigationController
let third = mainStoryBoard.instantiateViewControllerWithIdentifier("thirdNavController") as! UINavigationController

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let controllers = [first, second, third]

tabBarController.viewControllers = controllers
appDelegate.window!.rootViewController = tabBarController
appDelegate.window!.makeKeyAndVisible()

}

关于ios - 无法从 AppDelegate 中实例化 rootViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32845348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com