如果 ViewController1
未显示,但 ViewController2
显示:我可以轻松地从 ViewController2
更新 ViewController1
中的 GUI但是在显示 ViewController2
时,我无法检索 ViewController1
中的任何值。当未显示 ViewController1
时,它总是返回 nil 属性。
在我的例子中,每个 View Controller 都显示在标签栏 View Controller 上。
解决此问题的最佳方法是什么?谢谢。
最佳答案
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 :
EDIT 根据以下评论:
如果您正在使用 UITabBarController
,它有一个 viewControllers
属性,这样您的 View Controller 的所有保留计数就不会减少到 0,因为它还没有被释放。如果您发现任何nil
,那么它应该是您的初始化程序中的一些问题。
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITabBarController_Class/#//apple_ref/occ/instp/UITabBarController/viewControllers
你不应该有自己的机制来访问 View Controller 。相反,使用 UITabBarController
中的 viewControllers
属性,因为它有助于管理有效的 View Controller 。
关于ios - 未显示 UIViewController 时,属性返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34488224/