views:

260

answers:

2

I've created some custom UITableViewCells in a nib file and would like to use that across multiple UIViewControllers.

Can anyone tell me the best practice way to do that? My limited knowledge around loading nibs seems to suggest that you have to specify a single owner class in Interface Builder.

Thanks.

+1  A: 

Just do it; there are no real obstacles. As for the class for File's Owner, so long as the keys you bind to in the nib exist in any object you want to use the nib in, you will be ok.

Paul Lynch
I think you could also specify an `@interface` for File's Owner, if you want to do it a little more cleanly.
Kristopher Johnson
That's a great tip Kristopher. Thanks guys.
colm
+1  A: 

You can instantiate a UIViewController class programmatically with initWithNibName:bundle: and specify the same nib for multiple controllers. For example, I'm building a game right now that has a GameController class that defines the basic game logic in it. The GameController has an associated GameController.xib file that gets loaded up in a custom initializer:

- (id)initWithOptions:(NSDictionary *)gameOptions
{    
    if (self = [super initWithNibName:@"GameController" bundle:nil])
    {
        // code here
    }
    return self;
}

I have a few different game types: peer-to-peer, local, & online. The game logic is the same, but the communication implementation is slightly different, so each of these is a subclass of GameController. Based on how the application is used, it'll instantiate a different controller:

local = [[LocalGameController alloc] initWithOptions:options];
online = [[OnlineGameController alloc] initWithOptions:options];

You can see though that since these both extend GameController, that both of these will be init'ed with GameController.xib as its view. In this case, GameController would be your single file owner.

Typeoneerror