Why are controls (null) in awakeFromNib?
This is a follow on from another question regarding why I could not set UIControls in awakeFromNib. The answer to that is that as you can see below the controls are nil in the awakeFromNib
, although they are initialised to the correct objects by the time we get to viewDidLoa开发者_开发技巧d
. I setup the view the same as I always do, should I be doing something different to access them here, the xib(nib) was designed and saved with the current version of Image Builder.
CODE:
@interface iPhone_TEST_AwakeFromNibViewController : UIViewController {
UILabel *myLabel;
UIImageView *myView;
}
@property(nonatomic, retain)IBOutlet UILabel *myLabel;
@property(nonatomic, retain)IBOutlet UIImageView *myView;
@end
.
@synthesize myLabel;
@synthesize myView;
-(void)awakeFromNib {
NSLog(@"awakeFromNib ...");
NSLog(@"myLabel: %@", [myLabel class]);
NSLog(@"myView : %@", [myView class]);
//[myLabel setText:@"AWAKE"];
[super awakeFromNib];
}
-(void)viewDidLoad {
NSLog(@"viewDidLoad ...");
NSLog(@"myLabel: %@", [myLabel class]);
NSLog(@"myView : %@", [myView class]);
//[myLabel setText:@"VIEW"];
[super viewDidLoad];
}
OUTPUT:
awakeFromNib ...
myLabel: (null)
myView : (null)
viewDidLoad ...
myLabel: UILabel
myLabel: UIImageView
Much appreciated ...
gary
iboutlet was connect to control that was subview of view
and view
was lazy load
so you can access it first like the following:
func awakeFromNib() {
super.awakeFromNib()
let _ = self.view
println("\(self.myLabel)")
}
This can happen, for example, if the View for the ViewController is loaded from another NIB. These connections are initialized and fixed only when that NIB is actually loaded, because they don't exist in the NIB that actually instantiates and awakens your controller in the first place.
精彩评论