Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 749
TesX Avatar asked Aug 06 '10 10:08

TesX


People also ask

What is viewDidLoad in Swift?

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.

What is the purpose of viewDidLoad?

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.

What is the difference between viewDidLoad and viewDidAppear?

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.

How many times is viewDidLoad called?

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.


2 Answers

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

like image 141
Chintan Patel Avatar answered Oct 12 '22 11:10

Chintan Patel


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.

like image 45
Mike Abdullah Avatar answered Oct 12 '22 12:10

Mike Abdullah