Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
676 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...