Best way to configure NSViewController initialized by initWithNibName: bundle :?

I have a subclass of NSViewController that loads its view from nib (with initWithNibName: bundle: and is the owner of this nib file).

I need to do some initialization after loading nib, and I want my code to be the most compatible:

  • In ios: there is a viewDidLoad method for this
  • In osx: the snow leopard does not have a method such as viewDidLoad, but awakeFromNib is called on the owner of the file with the inscription

So my questions are:

  • Is awakeFromNib also called the owner of the nib file in Lion?
  • If I use awakeFromNib, do I need to call [super awakeFromNib]? (NSViewController implements awakeFromNib?)
  • If answer 1 is YES, is that 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 that 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 

thanks

+8
initialization ios nsviewcontroller macos awakefromnib
source share
1 answer

Here is what I found:

  • Yes, awakeFromNib : also called by the owner of the nib file in Lion (and usually this is the same for the new Mountain Lion).

  • Starting with 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 prior to 10.6, we can use instancesRespondToSelector : to find out if the parent class awakeFromNib , a subclass of NSView or NSObject should not call [super awakeFromNib] .

+4
source share

All Articles