viewDidLoad in NSViewController?

前端 未结 6 514
走了就别回头了
走了就别回头了 2020-12-01 02:15

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

How can I do that with NSViewController?

6条回答
  •  一整个雨季
    2020-12-01 02:47

    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

提交回复
热议问题