使用 Xib 将 UIView 添加到 UIViewController

发布于 2024-10-14 00:13:05 字数 366 浏览 1 评论 0原文

这就是我想要做的:

  1. 我有 AViewController ,它是 Xib,然后我添加一个 UIView 一半的屏幕,我将此视图连接到 B-UIView 和 B- UIView 有自己的 xib,我在那里进行设计。

换句话说,我有一个视图控制器,它的视图成为“主视图”,因为我向其中添加了带有自己的 xib 的其他视图。

层次结构:

1 ViewController UIView (xib) 2.B-UIView(有自己的xib) 3.C-UIView(有自己的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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从﹋此江山别 2024-10-21 00:13:05

是的,你绝对可以做到,而且一点也不难!

事实上,这是在 Interface Builder 中设计某些东西的一种非常常见的方法。然后,您可以使用 loadNibNamed 以编程方式加载自定义 Xib 文件。例如:

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

Apple 的文档在“iOS 的表视图编程指南”中讨论了这一点。

尽管他们的示例是关于创建自定义表格单元格,但相同的规则适用于任何视图。仔细查看标题为 从 Nib 文件加载自定义表格视图单元

在该示例中他们没有做的一件事是加载顶级视图。 loadNibNamed 返回一个数组,因此要获得您想要的顶级视图,您还应该这样做:

   MyCustomView *myCustomView = [myNibsArray objectAtIndex:0];

因此,您的代码将如下所示:

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文