Either AppDelgate is called or ViewController is

别说谁变了你拦得住时间么 提交于 2019-12-23 04:40:21

问题


I am wanting both my AppDelegate and my ViewController to be called on startup (as expected).

When only the ViewController is called:

main.c

int main(int argc, const char * argv[]) {
    return NSApplicationMain(argc, argv);
}

When the AppDelegate is called and the view controller is not.

main.c

#import "AppDelegate.h"

int main(int argc, const char *argv[])
{
    NSArray *tl;
    NSApplication *application = [NSApplication sharedApplication];
    [[NSBundle mainBundle] loadNibNamed:@"ViewController" owner:application topLevelObjects:&tl];

    AppDelegate *applicationDelegate = [[AppDelegate alloc] init];      // Instantiate App  delegate
    [application setDelegate:applicationDelegate];                      // Assign delegate to the NSApplication
    [application run];                                                  // Call the Apps Run method

    return 0;       // App Never gets here.
}

I think my problem is that with the second attempt of the main.c is that...

NSApplication *application = [NSApplication sharedApplication];
        [[NSBundle mainBundle] loadNibNamed:@"ViewController" owner:application topLevelObjects:&tl];

is not working as expected. Where do I actually set the 'Nib' in the IB?

What is wrong?


回答1:


You've got this kind of confused.... you should look more closely at my other answer over here: Other Answer

Your first main.c example is completely wrong. Your second main.c, which is closer to the example I gave is better, but you're calling viewController where you should be referencing the MainMenu nib, as in my example.

This line is wrong:

 [[NSBundle mainBundle] loadNibNamed:@"ViewController" owner:application topLevelObjects:&tl];

put the NIB name there... that's why the method argument is called "loadNibNamed" :)- and get rid of @"ViewController".

Now if you are trying to call a full blown nib, then you don't really need my example. That was designed for people who are really trying to be almost completely programmatic.



来源:https://stackoverflow.com/questions/32865162/either-appdelgate-is-called-or-viewcontroller-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!