views:

363

answers:

1

I've managed to get my myself confused... I've got a fairly complex View Controller much of which is developed programatically. I'd like to pop up a "dialog" on top of the view controller at some point and I'd like to "design" that view in Interface Builder because it's fairly straightforward (background UIImageView, some UILabels and a UIButton).

I've created the .xib and am now subclassing UIView with the IBOutlets,etc. I'm now trying to wire it up and realizing I probably need to add an initWithNibName: method so this will instantiate correctly...then I realize that I'm really just making another UIViewController and I don't think we're supposed to have UIViewController views w/in other UIViewController views?!?

So now I'm re-thinking how to go about this "correctly." How best to use IB to design a simple 1/4 screen view that will pop up over a main view?

A: 

Call +[NSBundle loadNibNamed:owner:] to load the NIB file that contains your view. If you specify your view controller (i.e., self) as the owner, any connections you make to File's Owner in the NIB file will then be made to the view controller. So you could declare an outlet to your custom view in the view controller and after the call

[NSBundle loadNibNamed:@"MyView" owner:self];

the outlet variable will point to the view object. Alternatively, you can use -[NSBundle loadNibNamed:owner:options:], which returns an array of the top-level objects in the NIB.

Ole Begemann
OK. trying with *[[NSBundle mainBundle] loadNibNamed:@"DialogView" owner:self options:nil];* but getting error that "this class is not key value coding-compliant for the key nameLabel". nameLabel is the name of the IBOutlet UILabel that I've wired up in IB to test this...
Meltemi
So it's trying to wire these items up with properties in the ViewController where i'm calling loadNibNamed:owner:options: from. But these properties are not in the VC they're in a DialogView which is a subclass of UIView. my VC has a property for dialogView. I guess I'd like this loadNibNamed to wire things up to a to a key path from my View Controller? I think I'm getting more confused!?!
Meltemi
The error message suggests that you haven't set the class of the object you are connecting nameLabel to correctly in IB. Have you set the File's Owner of DialogView.xib in IB to the class of your view controller? Is the class of the custom view DialogView in IB?
Ole Begemann
Yup! That was it! IB is a wonderfully powerful thing but when I don't use it for a while it can trip me up... Thanks alot!
Meltemi