未显示 UIViewController 时属性 return nil

Properties return nil when a UIViewController is not being presented

如果 ViewController1 未显示,但 ViewController2 显示:我可以轻松地从 ViewController2 更新 ViewController1 中的 GUI,但我无法检索 [=11] 中的任何值=] 而 ViewController2 出现。当不显示 ViewController1 时,它总是 returns 属性为 nil。

在我的例子中,每个视图控制器都显示在标签栏视图控制器上。

解决这个问题的最佳方法是什么?谢谢。

The nib file you specify is not loaded right away. It is loaded the first time the view controller's view is accessed. If you want to perform additional initialization after the nib file is loaded, override the viewDidLoad method and perform your tasks there.

这是来自关于 initWithNibName 指定初始值设定项的文档:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instm/UIViewController/initWithNibName:bundle:

编辑 根据以下评论:

如果您使用的是 UITabBarController,它有一个 viewControllers 属性,这样您的视图控制器的所有保留计数就不会减少到 0,这还没有被释放。如果您发现任何东西 nil,那么它应该是您的初始化程序中的一些问题。 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITabBarController_Class/#//apple_ref/occ/instp/UITabBarController/viewControllers

您不应该有自己的机制来访问视图控制器。相反,使用 UITabBarController 中的 viewControllers 属性 因为它有助于管理有效的视图控制器。