18

I have been using UIViewControllers and initWithNibName with much success, basically using them as a convenient way to design the view with Interface Builder. Unfortunately I have built a hierarchy of views before noticing this line in the UIViewController documentation:

Note: You should not use view controllers to manage views that fill only a part of their window

My question is this: Having a very simple NIB that only has a UIView in addition to the default First Responder and Owning Object, what is the simplest way to load the UIView into my code?

I have not been able to get loadNibNamed:owner:options: to work at this point, but suspect the answer will involve it somehow.

Venk
  • 5,949
  • 9
  • 41
  • 52
Winder
  • 1,984
  • 5
  • 23
  • 33

1 Answers1

29

Yes, just call

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

You normally do this from the view controller you have set as File's Owner in the NIB. That way, you can declare an outlet for the view in the view controller which will automatically get connected when you load the NIB file. You don't even have to work with the return value of the method in this case.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • Thanks, I was missing the `mainBundle` part – Winder Apr 16 '10 at 00:53
  • 1
    Hi, a quick question: is this approach handling the case of having a CustomView class ? If the File's Owner is the view controller, from which the view is initialized, how do I actually hook all the elements in the nib (such as MyLabel) to the CustomView class IBOutlets ? – aneuryzm Jun 23 '11 at 08:32
  • Just the view's class to your custom class in IB and then connect the outlets. No difference to what you would always do. – Ole Begemann Jun 23 '11 at 08:50
  • but what do I have to do if this UIView is already build on the nib and I want to reuse it? – Vladimir Stazhilov Feb 11 '12 at 11:47
  • I'm a bit confused as to where the IBOutlets go for the custom view... Do they just go in the view controller? If so, what is the point in having a separate custom view because none of the code can be split off into a separate class anyway? – jowie Jul 04 '12 at 10:04
  • One benefit is potential reusability of the NIB on multiple screens/view controllers. Of course, now that iOS officially supports custom container view controllers, I would recommend to write your own container view controller for the subview in most cases. – Ole Begemann Jul 04 '12 at 10:34
  • IMO using outlets is a bit more confusing than just grabbing the array that is returned by loadNibNamed. You don't need to set any outlets then and your view will have greater reusability. Just make sure the views are defined with the correct classes (if subclassed) in the xib file. Oh and don't set any owner (set it to nil). – Jonny Jul 11 '12 at 11:07
  • FYI, `owner` can be `nil` if you need only the view. In that case, connects the outlets to the view's properties/methods instead of owner's properties/methods. – Khanh Nguyen Oct 07 '14 at 01:26