开发者

How to hook up the view of a view controller into another xib file?

I made a fresh view-based app project which has a MyProjectViewController.xib. Then I created AnotherViewController class with an acomanying XIB file. I opened MyProjectViewController.xib up in IB and dropped a "View Controller" object into my window. But now I want to hook the view of that view contro开发者_如何学运维llers up with the view of MyProjectViewController.xib. I see no way of doing that in IB. Must I do that programmatically?

The thing is, AnotherViewController will be created with an XIB right? I think I must hook it up somehow otherwise it's "lost in space". Hope you know what I mean...


You can create a UIView derived class (.h and .m), call it AnotherView in Xcode and then create a view XIB (also in Xcode). Open the XIB in IB and change the view's class identity to be the name of your UIView derived class, AnotherView. Customize the UIView in IB to your hearts content and then you load it as a subview in your view controller with something like this:

NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"AnotherView" 
                                               owner:self options:nil];

// Will need to #import "AnotherView.h" for this to work
AnotherView *view = (AnotherView*)[array objectAtIndex:0];
// Size the view to whatever you need it to be
[view setFrame:viewRect];
// Add it to the view hierarchy of the view controller's view.
[[self view] addSubview:view];

Let me know if you need any clarification.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜