views:

249

answers:

2

Hi,

This is kinda a hard question to describe.

I'm just starting to make an iPad app.
Now with the amount of real estate I have, I'm planning to have different but similar "views" to populate the space. (might be easier to think a newspaper site, where many of the columns are similar)
I would like to create a "view template" so I can reuse the view in the different spots.

Is it possible to design the view template in Interface Builder? (meaning I design the UIView in Interface Builder and then somehow I can just do [window addSubview:viewController.viewTemplate1]; multiple times?
If so how would I load that view template in the different places?
If you know of an example code / tutorial that does something similar, that would work too.

Thanks,
Tee

A: 

The simplest way is to create a XIB file with: - The view template you want. - An UIViewController subclass, that will act as File Owner. - Bind the UIView to the UIViewController subclass.

With this method (which is used widely in the Apple's samples), you can load as many as views you want and place them wherever you want.

Laurent Etiemble
+1  A: 

Strictly speaking, UIViewControllers, as Laurent suggested, are only meant for views that take up the whole screen. From the View Controller Programming Guide:

The one-to-one correspondence between a view controller and a screen is a very important consideration in the design of your application. You should not use multiple custom view controllers to manage different portions of the same screen.

The correct technique here would be to use the NSBundle method -loadNibNamed:owner:options:, which instantiates the contents of a NIB and returns an array of the top-level objects. In this case, for each of your views, you'd do something like this:

NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"MyViewTemplate" owner:nil options:nil];
UIView *newView = [nibObjects objectAtIndex:0];

[myViews addObject:newView];

// set up the view and add it to your content view and so on
Noah Witherspoon