3
votes

I have a XYZViewController (simple UIViewController in storyboard) that is loaded up with the default view. I have a type XYZView for which I have UIView in a .xib file.

In the XYZViewController class, I have defined property for XYZView as an IBOutlet. What is tricky is I don't know how to connect this property to the UIViewController in storyboard (or UIVIew in .xib file) such that —

  • the IBOutlet is connected to the right UIView
  • the view in the xib becomes an added subview for the default view of the UIViewController.

(I under the question sounds dodgy and/or I may not have the very right way to explain it, but that's the best I could.)

EDIT: Further clarification may make it easier. I just don't want to myself say:

XYZView *xyzView = [[XYZView alloc] initWithFrame...]; 

or

[self.view addSubview:xyzView];

Maybe that helps.

1
After the edit my answer remains unchanged. You still have the same options. :)Fogmeister

1 Answers

4
votes

OK, from what I tell you have the following...

XYZViewController The code of this is in XYZViewController.h and .m files.

A .storyboard file In the storyboard file you have a view controller that you have set the subclass to XYZViewController.

A .xib file In the xib file you have a single view that you have defined as the subclass XYZView.

Right?

I'm guessing what you have done is the following...

In the .xib file you have laid out the XYZView and put labels, buttons, etc... on it.

The view controller is being created by the storyboard. But now you want to attach the labels and buttons to it.

Right?

If all this is correct then you have a couple of options.

The easiest option

Drop the xib file. Unless that XYZView is being used in multiple places in the app (i.e. inside different view controllers) then you should really be doing all of that layout in the storyboard. Add the buttons and labels to the XYZViewController in the storyboard.

This will then allow you to connect the IBOutlets and IBActions and it will all just work because the storyboard is creating and then setting the outlets and actions.

Next option

Because you have created the view in a xib file you have to load it from that xib file in code and then add it to you view controller.

Something like...

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.xyzView = [[[NSBundle mainBundle] loadNibNamed:@"XYZView" owner:self options:nil] objectAtIndex:0];

    [self.view addSubview:xyzView];
}

Then you can do stuff like ...

self.xyzView.someLabel.text = @"This is the text";

You still won't be able to connect outlets and actions but that's because the view controller is not being created by the xib. It's being created by the storyboard.

This can get all messy though. I'd really only recommend creating a view in a separate xib if it's something that you reuse over and over (like a 5star rating view or something).

What you absolutely can't do

OK, I think I may have thought of what you are doing.

In the storyboard you have set the subclass of the view as XYZView and you are expecting it to pick up the labels and buttons etc... that you have defined in the xib file for XYZView.

This absolutely will not work, ever.

The storyboard and the xib are completely separate objects. If you want to use them together then code is involved in loading a view from a nib and then adding it to a view controller created in a storyboard.