views:

19

answers:

1

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

+1  A: 

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.

jshier
Thank you for the post, it did at least show me that it should work. What I was missing was that I have created the controller and the *.xib files separately. What I did not do was set the file's owner for each *.xib to ColorController and connect it's view outlet to the view in Interface Builder.
fuzzygoat
I'm sorry. Reading this again it is much more unhelpful than I intended. Suffice to say that my point was that you needed to establish the view in some way, whether manually or through the nib.
jshier