fuzzygoat
fuzzygoat

Reputation: 26223

Dynamically loading NIBs?

Rather than having three separate controllers and their associated *.xib files I am trying to setup a generic controller and then instantiate it with one of three different xib files RED.xib" "GREEN.xib" & "BLUE.xib"

NSString *nibColor;
switch (selectedRow) {
    case 0: 
        nibColor = @"RED";
        break;
    case 1:
        nibColor = @"GREEN";
        break;
    case 2:
        nibColor = @"BLUE";
        break;
}

ColorController *colorController = [[ColorController alloc] initWithNibName:nibColor bundle:nil];

My problem is that I am not linking the view and get the following error.

loaded the "RED" nib but the view outlet was not set.

I understand that normally you link the view in IB, but is there a way to dynamically pick the nib at runtime, or do I need to create separate redController, blueController and greenControllers?

cheers Gary

Upvotes: 0

Views: 313

Answers (1)

Jon Shier
Jon Shier

Reputation: 12770

From Apple's UIViewController docs, which I'm assuming ColorController is a subclass of:

When you define a new subclass of UIViewController, you must specify the views to be managed by the controller. There are two mutually exclusive ways to specify these views: manually or using a nib file. If you specify the views manually, you must implement the loadView method and use it to assign a root view object to the view property. If you specify views using a nib file, you must not override loadView but should instead create a nib file in Interface Builder and then initialize your view controller object using the initWithNibName:bundle: method. Creating views using a nib file is often simpler because you can use the Interface Builder application to create and configure your views graphically (as opposed to programmatically). Both techniques have the same end result, however, which is to create the appropriate set of views and expose them through the view property.

Upvotes: 1

Related Questions