How to load a UIView using a nib file created with Interface Builder

There is also an easier way to access the view instead of dealing with the nib as an array.

1) Create a custom View subclass with any outlets that you want to have access to later. –MyView

2) in the UIViewController that you want to load and handle the nib, create an IBOutlet property that will hold the loaded nib’s view, for instance

in MyViewController (a UIViewController subclass)

  @property (nonatomic, retain) IBOutlet UIView *myViewFromNib;

(dont forget to synthesize it and release it in your .m file)

3) open your nib (we’ll call it ‘myViewNib.xib’) in IB, set you file’s Owner to MyViewController

4) now connect your file’s Owner outlet myViewFromNib to the main view in the nib.

5) Now in MyViewController, write the following line:

[[NSBundle mainBundle] loadNibNamed:@"myViewNib" owner:self options:nil];

Now as soon as you do that, calling your property “self.myViewFromNib” will give you access to the view from your nib!

Leave a Comment