开发者

Add a UIView to a UIViewController with Xib

Here is what i am trying to do:

  1. I have AViewController and it's Xib, then I add a UIView half the screen which I开发者_开发知识库 connect this view to B-UIView and B-UIView has its own xib where i do the design.

In other words I have a view controller with it's view which becomes the "mainview" as I add other views to it which come with its own xibs.

Hierarchy:

1 ViewController UIView (xib) 2.B-UIView (has its own xib) 3.C-UIView (has its own xib)

So is this possible if so how?

Many thanks in advance!


Yes, you can definitely do this and it's not difficult at all!

In fact, it's a pretty common way to design something in Interface Builder. You can then load your custom Xib file programmatically using loadNibNamed. For example:

[[NSBundle mainBundle] loadNibNamed:@"TVCell" owner:self options:nil];

Apple's docs talk about this in the "Table View Programming Guide for iOS".

Although their example is about creating a custom table cell, the same rules apply for any view. Have a close look at the section entitled Loading Custom Table-View Cells From Nib Files.

One thing they don't do in that example is to load the top level view. loadNibNamed returns an array, so to get that top level view that you're after, you should also do:

   MyCustomView *myCustomView = [myNibsArray objectAtIndex:0];

So, your code will look like:

   NSArray *myNibsArray = [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil];
   MyCustomView *myCustomView = [myNibsArray objectAtIndex:0];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜