Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can do this, and pretty easily. The way I do this is create a UIView subclass (let's say it's called "MyView.m" and "MyView.h"), and then a nib called "MyView.xib".</p> <p>First, in the nib, click File's Owner, and set the class to "MyView". This will make it so IBOutlets/Actions from your class show up in IB. Add a single top-level view (if it doesn't have one already) as your main view, and add your other custom elements (UILabels and UIImageViews) as subviews of the main UIView.</p> <p>Next, add the following code so that it gets called when MyView is initialized (remember, if you initialize from a nib you'll get inited from - (id)initWithCoder:(NSCoder *)aDecoder).</p> <pre><code>NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil]; UIView *mainView = [subviewArray objectAtIndex:0]; //Just in case the size is different (you may or may not want this) mainView.frame = self.bounds; [self addSubview:mainView]; </code></pre> <p>What this does is loads the view hierarchy from your nib into the NSArray, and since you only had one top-level UIView, you just add that as a subview of your custom UIView.</p> <p>This allows you to design UIViews (and other subclasses of UIView) in Interface Builder.</p> <p>EDIT: Updated to OP's edit.</p> <p>The way I've always done this is by first following my directions above. One difference is that in your ButtonTest nib you're changing the class of the UIView to ButtonTest, but I change the File Owner's class to ButtonTest. Then, inside <code>ButtonTest.m</code>, do the <code>loadNibNamed</code> stuff. Now, to add that object to <code>ButtonTestViewController.xib</code>, add a UIView (or UIButton, whatever ButtonTest is subclassed from) and change the class to ButtonTest.</p> <p>This is a little confusing, so I'll try to break it up by file.</p> <p><code>ButtonTestViewController.xib</code>: Add a UIButton (of whatever ButtonTest inherits from) and change the class to ButtonTest</p> <p><code>ButtonTest.m</code>: Inside the "initWithCoder" method, do all of the "loadNibNamed" stuff I have above</p> <p><code>ButtonTest.xib</code>: Change File's Owner class to ButtonTest and link up all IBOutlets and IBActions</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