Frage

I'm fairly experienced with iOS, but still pretty green with MacOS X Cocoa.

I am using InterfaceBuilder to create a view hierarchy. This is separate from the MainMenu NIB.

I am also creating an NSViewController. Because of the way that Mac OS X works, this controller object is not directly attached to the view hierarchy.

I connect all the various outlets in IB, making the view hierarchy the view object for the view controller, and attaching a few contained controls and whatnot.

Now, here's the problem: In iOS, I simply instantiate the controller from the storyboard, and the whole kit and kaboodle comes in with it. No fuss, no muss.

In Mac OS X, I can create the view controller:

MyAwesomeViewControllerClass *pViewController = [[MyAwesomeViewControllerClass alloc] initWithNibName:nil bundle:nil];

I can't find a Mac OS X analogue to the instantiateViewControllerWithIdentifier: method in iOS. That's really what I need.

If I need to instantiate them separately, I need to figure out how to do that.

I do not want to do this programmatically. There's a lot of autolayout stuff involved, and that is a substantial (and delicate) bundle of spaghetti.

Most likely, I am going about this wrong, and would like to purchase a clue for five bucks, Alex...

War es hilfreich?

Lösung

You will need to pass the name of NIB with that call to initWithNibName:bundle::

MyAwesomeViewControllerClass *pViewController = [[MyAwesomeViewControllerClass alloc]
    initWithNibName:@"MyAwesomeViewControllerClass"
             bundle:nil];

I usually override init:

- (instancetype)init {
    // NOTE: Not [self initWithNibName:bundle:] !!!
    self = [super initWithNibName:@"MyAwesomeViewControllerClass" bundle:nil];
    if (self) {
        // Other init
    }
    return self;
}

and then:

MyAwesomeViewControllerClass *pViewController =
    [[MyAwesomeViewControllerClass alloc] init];

is easier to type and doesn't require users of the view controller to know the NIB being used (even if it is obvious).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top