calling initWithNibName doesn't initialize items in the nib, it has 0x0

谁都会走 提交于 2019-12-03 12:34:49

问题


When I call:

self.viewController = [[DidItViewController alloc] initWithNibName:@"DidItViewController" bundle:nil];

and then I check self.viewController.navController right after this line is executed in the debugger, I find that it's empty (0x0).

On DidItViewController I have my navController defined as:

IBOutlet NavigationController *navController; 

and in my nib file I have the NavigationController bound to this navController property on the File Owner (a DidItViewController).

Why doesn't my navController get created? Any ideas? I think I may be missing something about the way initWithNibName works..

Thanks.


回答1:


Or you can use [NSBundle loadNibNamed:owner:options:] method instead of. This method ensures all outlet connections connected. (which [UIViewController initWithNibName: bundle:] doesn't)

Sample code

In this case, File's Owner in the NIB is an external instance of PhotoShow class.

// This works completely. All outlets works.
PhotoShow* obj = [[PhotoShow alloc] init];
[[NSBundle mainBundle] loadNibNamed:@"PhotoShow" owner:obj options:nil];
// Outlets are always available at this moment.

// This works. but does not connects outlets correctly sometimes.
PhooShow* obj = [[PhotoShow alloc] initWithNibName:@"PhotoShow" bundle:[NSBundle mainBundle]];
// Outlets may not available at this moment.

Selection from reference document

You can use this method to load user interfaces and make the objects available to your code. During the loading process, this method unarchives each object, initializes it, sets its properties to their configured values, and reestablishes any connections to other objects. (To establish outlet connections, this method uses the setValue:forKey: method, which may cause the object in the outlet to be retained automatically.) For detailed information about the nib-loading process, see Resource Programming Guide.




回答2:


It may take a bit of time for the xib to load all of the components. Only after viewDidLoad can you be sure that the navController is initialized




回答3:


The viewDidLoad method may not called when use this method.

PhotoShow* obj = [[PhotoShow alloc] init];
[[NSBundle mainBundle] loadNibNamed:@"PhotoShow" owner:obj options:nil];



回答4:


You should not be defining a "navController" property; all UIViewControllers have a "navigationController" and "navigationItem" property already automatically defined. These will point to the navigation controller and the navigation item respectively, assuming the view is on a navigation controller stack.

As has been previously stated, though, the "navigationController" property cannot be relied upon until the "viewDidLoad" function has been called. You should override your "viewDidLoad" method in "DidItViewController" to do whatever manipulation you intend to do with the navigation controller.

EDIT:
See: UINavigationController* UIViewController::navigationController()
See: UINavigationItem* UIViewController::navigationItem()



来源:https://stackoverflow.com/questions/1912079/calling-initwithnibname-doesnt-initialize-items-in-the-nib-it-has-0x0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!