views:

385

answers:

2

What is the best way to create a custom UIView that I can consume in Interface Builder?

  1. Create a custom UIView in Interface Builder and inherit from UIView in a code file, then somehow use it in another UIView ala like a control. (How do I do this?)

  2. Create a custom UIView in Interface Builder and have a custom UIViewController wire it up. In my main ViewController, place the new view.

Basically, I am trying to create a reusable display view and would like a quick way to change it across all my instances with minimal effort. I already have laid out my XIB in Interface Builder.

A: 

You'll want to do (1). Presumably you've got an existing IB file in which you'd like to place the custom UIView subclass? In that case, go to that file, drag out a UIView, and then in the "Application Identity" tab of the inspector (4th tab) set the Class to your custom class (as defined in code).

Ben Gottlieb
I have put a `UIView` (and set the class to `MyCustomUIView`) on to `MainView`, but the `UILabel` of `MyCustomView` don't show up. Is there something I need to do in `MyCustomUIView`?
Daniel A. White
Your MyCustomUIView should be all code; if you want to have a custom view that has it's own nib, there's a bit more work involved.
Ben Gottlieb
@Ben Gottlieb: Could you give a pointer re the required "a bit more work"? I asked a similar (but badly formulated) question a while ago but got no satisfactory answer.
Felixyz
this is a non-trivial exercise. It's very similar to setting up a UITableViewCell using a nib. Bill Dudney has a great blog post (http://bill.dudney.net/roller/objc/entry/uitableview_from_a_nib_file)on this.
Ben Gottlieb
@Ben Gottlieb: Many thanks, Ben. I've been wanting to learn these techniques for a while. Will be interesting to read the tutorial closely.
Felixyz
A: 

The best is the 1st way. And don't forget to place IBOutlet keyword before class member, that you want to see in Interface Builder.

@interface MyViewController : UIViewController {
    IBOutlet UILabel *m_MyLabel;
}
....
kovpas