Objective-C nil Outlets
-
24-10-2019 - |
Question
I have a class used for a user interface, with two constructors:
- (id)initWithBanner:(NSMutableArray *)banner {
if ( ( self = [super initWithNibName:@"UIBanner" bundle:nil] ) ) {
// ... code...
}
return self;
}
- (id)initWithPreview:(NSMutableArray *)previews {
if ( ( self = [super initWithNibName:@"UIBanner" bundle:nil] ) ) {
// ... code...
}
return self;
}
Inside this two constructors, I use two outlets, a UIPageControl and a UIScrollView, linked with the new XCode 4. Now, if I use the first constructor, initWithBanner, everything works fine (putting a NSLog(@"%@",bannerScroll) gives the relative outlet description) but when I use initWithPreview, my Outlets are nil. What's wrong with that?
Solution
Your outlets will not be set until the nib is actually loaded, which occurs when your UIViewController's view
property is read. You implement the -viewDidLoad
method to handle when your nib has loaded. Also note that the view and nib can be unloaded and loaded multiple times during the life of your UIViewContoller instance.