viewDidLoad in NSViewController?

On the iPhone I use UIViewController's viewDidLoad to run code to set up the view.

How can I do that with NSViewController?

I've tried loadView but it doesn't work...


I figured it out within minutes of posting my comment. Adding my finding as an answer because it is an example which is missing in the docs. The below code will give you the viewDidLoad method that you want. Its so easy in a way that i wonder why Apple has not implemented it yet in OS X.

- (void)viewWillLoad {
    if([NSViewController instancesRespondToSelector:@selector(viewWillLoad)]) {
        [super viewWillLoad];
    }

    ...
}

- (void)viewDidLoad {
    if([NSViewController instancesRespondToSelector:@selector(viewWillLoad)]) {
        [super viewDidLoad];
    }
}

- (void)loadView {
    BOOL ownImp = ![NSViewController instancesRespondToSelector:@selector(viewWillLoad)];

    if(ownImp) {
        [self viewWillLoad];
    }

    [super loadView];

    if(ownImp) {
        [self viewDidLoad];
    }
}

Original source: http://www.cocoabuilder.com/archive/cocoa/195802-garbage-collection-leaks-and-drains.html


As of OS X 10.10, viewDidLoad is available and supported on NSViewController.

Prior to that, you had to go by this nugget in Snow Leopards' release notes:

Advice for People who Are Looking for -viewWillLoad and -viewDidLoad Methods in NSViewController

Even though NSWindowController has -windowWillLoad and -windowDidLoad methods for you to override the NSViewController class introduced in Mac OS 10.5 does not have corresponding -viewWillLoad and -viewDidLoad methods. You can override -[NSViewController loadView] to customize what happens immediately before or immediately after nib loading done by a view controller.


As of OSX 10.10 (Yosemite), there is now a -viewDidLoad, -viewWillAppear, -viewDidAppear, -viewWillDisappear in NSViewController. See WWDC 2014 - Storyboards and Controllers on OS X session for more info, to find out when each of them gets called, etc.

Here's the relevant bit from the 10.10 header docs about -viewDidLoad:

Called after the view has been loaded. For view controllers created in code, this is after -loadView. For view controllers unarchived from a nib, this is after the view is set. Default does nothing.

- (void)viewDidLoad NS_AVAILABLE_MAC(10_10);