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...
This method is called after the view controller has loaded its view hierarchy into memory. This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView() method.
Use viewDidLoad( ), which is called AFTER loadView( ) has completed its job and the UIView is ready to be displayed. viewDidLoad( ) allows you to initialize properties of the view/viewController object and finalize them before viewWillAppear( ) is called.
The difference between viewDidAppear and viewDidLoad is that viewDidAppear is called every time you land on the screen while viewDidLoad is only called once which is when the app loads.
viewDidLoad() is one of the initialization methods that is called on the initial view controller. viewDidLoad() is called before anything is shown to the user - and it is called only once.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With