Before 10.10 the NSViewController was basically doing nothing (it was the NSWindowController doing most of the job) ;) On 10.10 it gained the methods similar from iOS world (viewDidLoad...) and one really important thing, it was finally put automatically into NSResponder chain (NSApp sendAction:... will check if the viewController has the method).
So how to get the functionality?
- use awakeFromNib
- manually add view into responder chain (if you compile with 10.10 SDK then the view is added automatically on 10.10 and later; if you compile with 10.9 SDK you have to add it manually)
- use XIBs; you can't use storyboards -> they use 10.10 nsviewcontrollers -> dead end
So if you use storyboards it's a lot of work. If you don't have clean MVC/MVVM architecture then it's not worth.
Alternative is to double your classes pre10.10 and after10.10 but this means having the same code layed off twice (once using storyboards, once using XIBs).
EDIT:
As @geowar suggested you can override loadView. Mind that you still can't use storyboards and you have to manually add your viewController into responderChain
- (void)loadView
{
[super loadView];
// if we're running on 10.8 or older…
if (NSAppKitVersionNumber <= NSAppKitVersionNumber10_8) {
[self viewDidLoad]; // call viewDidLoad (added in 10.9)
}
}
Alternatively
- (void)setView:(NSView*)view {
super.view = view;
// if we're running on 10.8 or older…
if (NSAppKitVersionNumber <= NSAppKitVersionNumber10_8) {
[self viewDidLoad]; // call viewDidLoad (added in 10.9)
}
}
@end
EDIT2:
awakeFromNib is called after loadView (which gets the new view from xib and instantiates it). As suggested by @Daij-Djan another solution is to override setter.
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.
