Either AppDelgate is called or ViewController is

2019-06-11 09:46发布

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条回答
闹够了就滚
2楼-- · 2019-06-11 10:15

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.

查看更多
登录 后发表回答