Loading a subview from a nib file in interface builder
I have a custom toolbar view which I'm storing in its own nib file. I want to use that toolbar in other nibs. Is it possible to just have a view of some sort as a subview in the main nib file, and load the toolbar nib into it? Preferably in interface builder.
开发者_运维问答Thanks
is the custom toolbar present in a separate nib file? If yes, then you can load it independently anytime with
NSArray *arr = [[NSBundle mainBundle] loadNibnamed:@"customToolBarNibName" owner:... options:...];
UIView *rootView = [arr objectAtIndex: 0];
It sounds like you have a single view in your nib file, and you want to be able to load an instance of that view at various places in your app.
Here's whatcha do:
First, in the nib file, change the File's Owner class to UIViewController, and set your custom view as the view property of the File's Owner.
Then, in the view controller where you want to use an instance of your custom view, do the following:
UIViewController *vc = [[UIViewController alloc] initWithNibName:@"YourCustomNib" bundle:nil];
UIView *yourCustomView = vc.view;
[vc release];
I know that seems kind of hacky, but last I heard that's actually one of the "right" ways of loading a single view from a nib. I use this method when I have custom tableview cells in the nib.
精彩评论