Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nesting XIBs in Interface Builder / embedding by reference

Say I have created an XIB for an icon view. I then want to embed a number of instances of this icon view in a different XIB container view by reference, so if I alter properties / layout in the original icon view XIB, the instances in the container view XIB reflect these changes.

Essentially, embedding by reference.

Maybe I'm being dense, but it seems the default behaviour of Interface Builder when dragging a view into a container view is to copy everything over, rather than referencing the original XIB? And dragging an instance of the class associated with the icon view into the container view just results in a blank view.

I'm sure there's a way to do this, but I'm damned if I can figure it out. I normally avoid IB like the plague ;)

like image 416
Henry Cooke Avatar asked Oct 07 '10 06:10

Henry Cooke


1 Answers

There isn't a way of doing this directly in Interface Builder. I needed to do something similar to this in my last app. What I ended up doing was just placing a placeholder View in the location where you want your referenced xib and then in your viewDidLoad or viewWillAppear, etc, you load that xib and place that loaded view as a child of your placeholder view.

NSArray *views = [[NSBundle mainBundle] loadNibNamed: @"ReferencedView" owner: self options: nil];
UIView *referencedView = (UIView *)[views objectAtIndex:0];
[self.viewPlaceholder addSubview:referencedView];

Alternatively, you can create an IBOutlet in your view controller to hold the referenced view and connect that outlet in IB. If you do, then you won't need to pull the view out of the NSArray.

like image 65
Kenny Wyland Avatar answered Oct 24 '22 18:10

Kenny Wyland