为什么 UIViewController 有 xib 文件而 UIView 没有?

发布于 2024-09-26 13:25:51 字数 233 浏览 0 评论 0原文

当我在 xcode 中创建一个新的 UIViewController 时,它会为我提供一个与界面关联的笔尖。但是,当我创建 UIView 时,它却没有。如果我对 MVC 的理解是正确的,视图实际上应该是包含界面元素(即笔尖)的部分,而视图控制器是将功能挂钩到它们控制的视图的部分。

我确信无论哪种方式我都能让它一起工作,所以这更像是一个探索性的问题。

这似乎是我缺少对如何使用这两个部分的一些基本理解的情况。我缺少什么?

When I create a new UIViewController in xcode, it offers to make me an associated nib for the interface. However, when I create a UIView, it does not. If my understanding of MVC is correct, views should really be the parts that contain the interface elements (i.e. the nib) while view controllers are the parts that hook the functionality to the views they control.

I'm sure I'll be able to get it working together either way, so this is more of an exploratory question.

This seems like a case where I'm missing some fundamental understanding of how these two parts should be used. What am I missing?

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

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

发布评论

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

评论(3

眼趣 2024-10-03 13:25:51

因为UIView通常不会这样使用。
但是 如何关联笔尖 (.xib )带有 UIView 的文件?

Because UIView is usually not used in such way.
However How do I associate a nib (.xib) file with a UIView?

枯叶蝶 2024-10-03 13:25:51

我最终得到的满足我兴趣的答案大致是这样的:

视图控制器的工作是管理视图层次结构。 Interface Builder 是创建视图层次结构的优秀工具。当您创建新的视图控制器时,Xcode 会提供创建 .xib,因为您很可能希望从 .xib 加载控制器的视图。

.xib 文件不一定要保存进入视图的每个 UIView(或子类),而只是在视图生命周期中不会更改的视图的一般轮廓。其他 UIView 更容易以编程方式创建,因为它们经常更改。

The answer I eventually got that satisfied my interest was roughly this:

The job of a view controller is to manage a view hierarchy. Interface Builder is an excellent tool for creating view hierarchies. Xcode offers to create a .xib when you create a new view controller because chances are high that you'll want to load the controllers' views from a .xib.

.xib files aren't necessarily meant to hold every UIView (or subclass) that goes into the view, just a general outline of views that don't change during the life of the view. The other UIViews are much easier to create programmatically since they change often.

素罗衫 2024-10-03 13:25:51

我也有类似的困惑。基本上(根据 MVC)视图包含在控制器内。在 iPhone 中,这意味着 UIViewController 有一个名为“view”的属性,它是一个 UIView 实例。

XIB 文件(这一点不常被提及)是一个序列化的 UIView 实例。它大致是一种 XML 子格式,表示 UIView 及其所有后续视图。因此,当您创建 UIViewController 时,会以 XIB 的形式创建 UIView 并绑定到该控制器。

因此,新的 UIView 没有 XIB,因为它们本质上是相同的东西......

I had a similar confusion. Basically (according to the MVC) the View is contained inside the Controller. In the iPhone this means that UIViewController has a property called 'view' which is a UIView instance.

A XIB file (and this is not mentioned often) is a serialised UIView instance. It is roughly an XML sub format which represents a UIView with all its subsequent views. So when you create a UIViewController, a UIView is created in the form of a XIB and bounded to that controller.

A new UIView therefore does not have a XIB because they are essentially the same thing...

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