Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add UIView on UIViewController

I want to add a "custom" uiview onto a uiviewcontroller, this custom view i created with xib and its a seperate from the view controller,

does anyone know how to add a uiview with a xib into a uiviewcontroller?

Many thanks in advance

like image 609
tosi Avatar asked Feb 22 '26 11:02

tosi


1 Answers

You mean an additional view, not the main controller view? In that case you can declare a property for the view and load the NIB by hand:

@interface Controller {}
@property(retain) IBOutlet UIView *extraView;
@end

…

- (void) viewDidLoad // or anywhere else
{
    [[NSBundle mainBundle] loadNibNamed:@"extras" owner:self options:nil];
    NSAssert(extraView != nil, @"The extra view failed to load.");
    [[self view] addSubview:extraView];
}

This assumes that you set the Controller as the file owner in the Interface Builder and you link the view to the extraView outlet. Also note that there might be more elegant solutions, like inserting the extra view into the main NIB for your controller; depends on the situation.

like image 163
zoul Avatar answered Feb 27 '26 08:02

zoul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!