viewDidLoad 调用两次,使用导航控制器 [英] viewDidLoad called twice, using navigation controller

查看:57
本文介绍了viewDidLoad 调用两次,使用导航控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ViewController 上的 ViewDidLoad 方法被调用了两次,但只在特定情况下调用.我需要展示两个视图控制器,一个如果用户未登录,第二个如果用户已登录.我正在使用故事板并在其中设置了一个导航控制器作为初始视图控制器.

My ViewDidLoad method on a ViewController is called twice, but only in a particular scenario. There are two view controllers which I need to present, one if user isn't logged in and the second if the user is logged in. I am using storyboard and have set a navigation controller as initial view controller in it.

在我的 AppDelegate didFinishLaunchingWithOptions 方法中,我用所需的控制器填充了 ViewControllers 数组,如下所示

In my AppDelegate didFinishLaunchingWithOptions method I have populated ViewControllers array with the desired controller as below

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = storyboard.instantiateViewController(withIdentifier: "navController") as! UINavigationController
if UserDefaults.standard.object(forKey: USERID) != nil {
    viewController = storyboard.instantiateViewController(withIdentifier: "HomeVC_ID") as! HomeVC
} 
else {
    viewController = storyboard.instantiateViewController(withIdentifier: "LoginVC_ID") as! LoginVC
}
navigationController.viewControllers = [viewController] as! [UIViewController]
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()

HomeVC 中的

ViewDidLoad 方法被调用两次,而 LoginVC 只调用一次.

ViewDidLoad method in HomeVC is called twice, whereas it's called just once for LoginVC.

我已经尝试搜索文章viewDidLoad 被调用两次viewDidLoad 在启动时在 rootViewController 上被调用两次,但无法解决问题.>

I already tried searching through articles viewDidLoad is called twice and viewDidLoad getting called twice on rootViewController at launch but couldn't corner the issue.

推荐答案

当你从故事板创建你的导航视图控制器时,它已经包含它的 rootViewController(不要与 rootViewControllerUIWindow 的 code>rootViewController).我猜这是你的 HomeVC(在故事板中).因此,故事板魔术已经创建了 HomeVC,您不必在 didFinishLaunchingWithOptions 中手动创建它.

When you create your navigation view controller from the storyboard, this already contains it's rootViewController (which must not to be confused with the rootViewController of the UIWindow). I guess this is your HomeVC (in the storyboard). So, the storyboard magic already creates HomeVC, and you do not have to create it manually in didFinishLaunchingWithOptions.

如果您已在项目/目标的属性中将故事板指定为您的主界面,则您不需要在 didFinishLaunchingWithOptions 中添加任何创建代码,只需让框架发挥作用即可.

If you have specify the storyboard as your main interface in the project's/target's properties, you do not need any creational code in didFinishLaunchingWithOptions and just let the framework perform the magic.

如果您想以编程方式执行此操作,那么 - 在情节提要中 - 您应该删除导航控制器,并在 didFinishLaunchingWithOptions 中手动创建(而不是通过 instantiateViewController).您还可以在此处添加适当的根视图控制器(从故事板实例化),可能如下所示:

If you want to do this programatically, then - in the storyboard - you should remove the navigation controller, and create it manually (not via instantiateViewController) in didFinishLaunchingWithOptions. You would also add the appropriate root view controller here (instantiated from the storyboard), maybe like this:

let storyboard = UIStoryboard(name: "Main", bundle: nil)

if UserDefaults.standard.object(forKey: USERID) != nil {
    viewController = storyboard.instantiateViewController(withIdentifier: "HomeVC_ID") as! HomeVC
} else {
    viewController = storyboard.instantiateViewController(withIdentifier: "LoginVC_ID") as! LoginVC
}
let navigationController = UINavigationController(rootViewController:viewController)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()

这篇关于viewDidLoad 调用两次,使用导航控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆