Igor Bizi
Igor Bizi

Reputation: 105

How to load a NIB from NIB in iOS

I have CustomViewController created via XIB.

I also created CustomView via different XIB file that is subclass os UIView.

Then I drug UIView to CustomViewController and set its class to CustomView.

Problem is here: then I run this app I did not see content of CustomView that I created in CustomView XIB.

Why and how can I fix it?

I need to have CustomView in CustomViewController because I need to set layout of this view.

I can load this CustomView from ViewDidLoad, but I need to load it from XIB

UIView *v = [[[NSBundle mainBundle] loadNibNamed:@"TopSegmentedControl" owner:self options:nil] firstObject ];
[self.view addSubview:v];

Upvotes: 2

Views: 1679

Answers (2)

Jef
Jef

Reputation: 4728

I think you should think about changing your UIView subclass so that it inherits from UIViewController, and is added to the hierarchy as a child ViewController. If you do that then you can add the containerView object from the inspector pallet there in interface builder into the outer viewControllers view, and it'll add the new (nib within a nib) there for you to paste everything into..

I think from recent changes in cocoa and cocoaTouch that Apple don't want us loading simple views from nibs. Note the 'files owner' proxy up top is now labelled 'ViewController'

Upvotes: 2

John Rogers
John Rogers

Reputation: 2202

You're going to have to put the code you included there into the initWithCoder: method of your UIViewController. This is the initialisation method that is called when it is initialised from a storyboard. Assuming you've connected an outlet to it named myView:

- (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        self.myView = [[[NSBundle mainBundle] loadNibNamed:@"TopSegmentedControl" owner:self options:nil] firstObject];

        return self;
    }
}

Upvotes: 1

Related Questions