问题
Let me first say that I've searched Google and, although many have similar issues, I haven't seen anything with the following bizarre behavior and remedy.
I've created a UIViewController and associated nib with several IBOutlets. On trying to consume this nib from another class, I discovered that after instantiating it with initWithNibName:bundle:, the IBOutlets are still nil.
I confirmed that they are correctly wired up, and yes, they are being synthesized, but still nothing. While investigating further, I changed the initWithNibName method as follows:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
UIView *view = self.view;
NSArray *subviews = self.view.subviews;
NSLog(@"Loaded view containing %d subviews.", [subviews count]);
}
return self;
}
Unbelievably, the addition of the three lines here, creating throwaway variables and logging, makes the IBOutlets wire up properly and work from the outside.
I have cleaned and rebuilt and restarted my machine, but still, if I remove these lines, it stops working. I am really baffled here and concerned that I have some kind of voodoo working that's going to break the moment I ship. Any ideas on what could be happening?
回答1:
You misunderstand what initWithNibName:bundle:
does, and when a UIViewController
loads its nib.
The initWithNibName:bundle:
method records the name of the nib to be loaded. It does not immediately load the nib.
The -[UIViewController view]
method loads the nib on demand, by sending [self loadView]
if necessary. The implementation of -[UIViewController view]
is basically this:
- (UIView *)view {
if (_view == nil) {
[self loadView];
[self viewDidLoad];
}
return _view;
}
So if you call self.view
in your initWithNibName:bundle:
override, that will cause the view controller to load its nib.
However, it's generally inappropriate to call self.view
from initWithNibName:bundle:
. A view controller should be able to exist without its view hierarchy.
For example, suppose a user is running your app and navigates through several view controllers, which you implement by pushing the view controllers onto a navigation controller. Then the user switches to another app for a while. Now the system is running low on memory, so it kills background apps - including your app.
When the user launches your app again, your app (if it's well done) should try to restore the user's state. That means reloading the navigation controller with the stack of push view controllers. But only the top view controller's view is visible on screen. It would be a waste of time, memory, and battery to reload the views for all of the hidden view controllers immediately. That's why each view controller loads its nib on demand.
来源:https://stackoverflow.com/questions/13593395/iboutlets-not-being-set-except-when-creating-throwaway-variables-during-initwith