Either AppDelgate is called or ViewController is
Asked Answered
P

1

0

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?

Picker answered 30/9, 2015 at 11:40 Comment(2)
Please read an introduction to Cocoa programming.Haakon
app will only get to return point only if you call terminate on default runloop (which you have triggered on previous line). Default app runloop is like a while (true) {shouldIDoSomething}. And give a brief look github.com/drichardson/examples/blob/master/NiblessCocoaApp/… You will have idea how to do it correctly.Headboard
O
0

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.

Oft answered 30/9, 2015 at 20:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.