i have a subclass of NSViewController that loads its view from a nib (with initWithNibName:bundle: and it is the file's owner of that nib).
I need to do some initialization after the nib is loaded and i want my code to be the most compatible :
- In ios : There is the viewDidLoad method to do that
- In osx : In snow leopard, there is no method like viewDidLoad but awakeFromNib is called on the file's owner of the nib too
So my questions are :
- Is awakeFromNib called also on the file's owner of the nib in Lion ?
- If i use awakeFromNib, do i need to call [super awakeFromNib] ? (is NSViewController implements awakeFromNib ?)
- If answer 1 is YES, is this a good solution ? :
- (void)initAfterNibLoaded { ... } - (void)viewDidLoad { // Code for ios [self initAfterNibLoaded]; } - (void)awakeFromNib { // Code for osx // Not sure if necessary [super awakeFromNib]; [self initAfterNibLoaded]; }
If answer 1 is NO, is this a good solution ? :
- (void)viewDidLoad {
// Initialize after nib loaded
}
#ifndef TARGET_OS_IPHONE
- (void)loadView {
// Call parent method
[super loadView];
// Simulate viewDidLoad method
[self viewDidLoad];
}
#endif
Thank you
Here is what I found:
Yes,
awakeFromNib
: is called also on the file's owner of the nib in Lion (and normally it's the same for the new Mountain Lion).Starting on OSX 10.6, there is a category on
NSObject
that addsawakeFromNib
, so it's safe to call[super awakeFromNib]
from any subclass. For OSX before 10.6, we can useinstancesRespondToSelector
: to know if the parent class implementsawakeFromNib
, theNSView
orNSObject
subclass must not call[super awakeFromNib]
.