Reputation: 16430
I'd like understand why if i try to set value (I.e. setAlphaValue or setTitle) for an object (like a NSButton) in init method nothing happen, but if i call setter function in awakeFromNib it works correctly.
@interface appController : NSObject {
NSButton *btn;
}
@end;
@implementation appController
-(void)awakeFromNib {
//it works
[btn setTitle:@"My title"];
}
-(id)init {
self = [super init];
if(self){
//it doesn't works
[btn setTitle:@"My title"];
}
}
@end
Upvotes: 16
Views: 17133
Reputation: 25632
When in init, the view will not be set up properly, and the outlets aren't connected. That's why you use awakeFromNib:
in this case - everything is set up and ready to be used.
Upvotes: 5
Reputation:
Outlets are set after -init
and before -awakeFromNib
. If you want to access outlets, you need to do that in -awakeFromNib
or another method that’s executed after the outlets are set (e.g. -[NSWindowController windowDidLoad]
).
When a nib file is loaded:
-init
, -initWithFrame:
, or -initWithCoder:
-awakeFromNib
is sent to interface objects, file’s owner, and proxy objects.You can read more about the nib loading process in the Resource Programming Guide.
Upvotes: 47