Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've created similar constructs except that I do not use the result in IB, but instantiate using the code. I'll describe how that works, and at the end I'll give you a hint how that can be used to accomplish what you're after.</p> <p>I start from an empty XIB file where I add one custom view at the top level. I configure that custom view to be my class. Below in view hierarchy I create and configure subviews as required.</p> <p>I create all IBOutlets in my custom-view class, and connect them there. In this exercise I ignore the "File's owner" completely.</p> <p>Now, when I need to create the view (usually in controller as part of while/for-loop to create as much of them as needed), I use NSBundle's functionality like this:</p> <pre><code>- (void)viewDidLoad { CGRect fooBarViewFrame = CGRectMake(0, 0, self.view.bounds.size.width, FOOBARVIEW_HEIGHT); for (MBSomeData *data in self.dataArray) { FooBarView *fooBarView = [self loadFooBarViewForData:data]; fooBarView.frame = fooBarViewFrame; [self.view addSubview:fooBarView]; fooBarViewFrame = CGRectOffset(fooBarViewFrame, 0, FOOBARVIEW_HEIGHT); } } - (FooBarView*)loadFooBarViewForData:(MBSomeData*)data { NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"FooBarView" owner:self options:nil]; FooBarView *fooBarView = [topLevelObjects objectAtIndex:0]; fooBarView.barView.amountInEuro = data.amountInEuro; fooBarView.barView.gradientStartColor = data.color1; fooBarView.barView.gradientMidColor = data.color2; fooBarView.titleLabel.text = data.name; fooBarView.titleLabel.textColor = data.nameColor; return fooBarView; } </code></pre> <p>Notice, how I set owner of nib to <code>self</code> - there's no harm as I didn't connect "File's owner" to anything. The only valuable result from loading this nib is its first element - my view.</p> <p>If you want to adapt this approach for creating views in IB, it's pretty easy. Implement loading of subview in a main custom view. Set the subview's frame to main view's bounds, so that they are the same size. The main view will become the container for your real custom view, and also an interface for external code - you open only needed properties of its subviews, the rest is encapsulated. Then you just drop custom view in IB, configure it to be your class, and use it as usual.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload