Question

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 :

  1. Is awakeFromNib called also on the file's owner of the nib in Lion ?
  2. If i use awakeFromNib, do i need to call [super awakeFromNib] ? (is NSViewController implements awakeFromNib ?)
  3. 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

Was it helpful?

Solution

Here is what I found:

  1. 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).

  2. Starting on OSX 10.6, there is a category on NSObject that adds awakeFromNib, so it's safe to call [super awakeFromNib] from any subclass. For OSX before 10.6, we can use instancesRespondToSelector: to know if the parent class implements awakeFromNib, the NSView or NSObject subclass must not call [super awakeFromNib].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top