I externalized a component in a XIB file so I can reuse it in several different views. Now in my view controller I load the XIB-based UIView and add it to the current view through :
NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"ThumbsPreviewView" owner:self options:nil];
_likePreview = [views objectAtIndex:0];
[self.view addSubview:_likePreview];
At first, I thought that it was not working... but actually it works but only put the root view (i.e. the container of all my graphical components, which is transparent) to my view.
So I need to put this kind of code (for every sub component) to make it work :
[self.view addSubview:_likePreview];
NSArray *subviews = _likePreview.subviews;
for(UIView * subview in subviews) {
[self.view addSubview:subview];
}
This is very counter-intuitive, and moreover I think it does not create the view hierarchy that I would like.
I am sure there is a solution because I did the same kind of thing in a UITableViewController to load a custom table header from a XIB. Using the exact same XIB file in this situation loads everything properly. Here is the code :
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
switch(section) {
case 0: {
NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"ThumbsPreviewView" owner:self options:nil];
_likePreview = [views objectAtIndex:0];
return _likePreview;
}
}
return [super tableView:tableView viewForHeaderInSection:section];
}
With the above code, everything is fine, I got the whole view (= root view container with all its content) as the header of my table section.
So the question is : what am I doing wrong in my first example ? How can I add all content (container + all content) to my parent UIView as a proper UIView hierarchy ?
EDIT : I need to precise that everything is organized in my XIB file in a single root UIView, as follows (I cannot post image) :
* Thumbs preview view
|_ Button
|_ Image View
|_ Image View
|_ Image View
|_ Image View
|_ Button
|_ Button
|_ Button
|_ Label
Thank you very much for your help !
Christophe
One way to load your component is to store your components into a top
UIView
object (s. attached screenshot).(source: mobypicture.com)
You can then load your nib as usual and use
-addSubview:
to add your components to the view hierarchy.EDIT:
Loading the nib with
and adding the view to the view hierarchy with
leads to the following view hiearchy: