为什么 UIWindow 既是 UView 的子级又是父级?

发布于 2024-07-19 05:18:38 字数 253 浏览 13 评论 0原文

UIKit 参考资料称 UIView 是 UIWindow 的超类,但尽管有这种血统,UIWindow 实际上管理 UIView。 这对我来说听起来很不寻常。

有谁知道这对软件设计有何意义?

非常感谢。

编辑:
我阅读了 iPhone 编程指南中的相关段落。 但我不明白为什么他们会做相反的事情:让 UIWindow 成为 UIView 的父级。 一定有什么事情迫使 Apple 以这种方式设计类层次结构。

The UIKit reference says UIView is a superclass of UIWindow, but despite of this parentage, a UIWindow actually manages UIViews. This sounds so unusual to me.

Does anyone know what the significants of this are in terms of software design?

Many thanks.

EDIT:
I read the related paragraph in the iPhone programming guide. Yet I couldn't see why they do the reverse: let UIWindow be the parent of UIView. There must have been some thing that forced Apple to design the class hierarchy this way.

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

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

发布评论

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

评论(3

把时间冻结 2024-07-26 05:18:38

来自 http://developer.apple.com /iPhone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/WindowsandViews/WindowsandViews.html

经验丰富的 Mac OS X 开发人员可能会发现 UIWindow 类不寻常的一件事是它的继承。 在Mac OS X中,NSWindow的父类是NSResponder。 在iPhone OS中,UIWindow的父类是UIView。 因此,在 iPhone OS 中,窗口也是一个视图对象。 尽管有其起源,您通常会像对待 Mac OS X 一样对待 iPhone OS 中的窗口。也就是说,您通常不会直接操作 UIWindow 对象的视图相关属性。

编辑:

UIView 是通用的(提供用于创建所有类型的视图并访问其属性的通用方法。)而 UIWindow 更具体(类定义管理和协调应用程序在屏幕上显示的窗口的对象。)

我知道有点模糊,我想只有苹果知道这个层次结构的确切原因。

http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html#//apple_ref/occ/cl/UIWindow

UIWindow 类定义管理和协调应用程序在屏幕上显示的窗口的对象(称为 windows )。 窗口的两个主要功能是提供一个用于显示其视图的区域以及将事件分发到视图。


http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/cl/UIView

UIView 类提供了用于创建所有类型的视图并访问其属性的常用方法。 例如,除非子类有自己指定的初始值设定项,否则您可以使用 initWithFrame: 方法来创建视图。 Frame 属性指定超级视图坐标中视图的原点和大小。 所有视图的坐标系原点都在左上角。

UIView 对象排列在 UIWindow 对象内的子视图的嵌套层次结构中。 视图层次结构中的父对象称为超级视图,子对象称为子视图。 视图对象声明其封闭超级视图的矩形区域,负责该区域内的所有绘制,并且也有资格接收其中发生的事件。 兄弟视图能够毫无问题地重叠,从而允许复杂的视图放置。

from http://developer.apple.com/iPhone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/WindowsandViews/WindowsandViews.html

One thing that experienced Mac OS X developers may find unusual about the UIWindow class is its inheritance. In Mac OS X, the parent class of NSWindow is NSResponder. In iPhone OS, the parent class of UIWindow is UIView. Thus, in iPhone OS, a window is also a view object. Despite its parentage, you typically treat windows in iPhone OS the same as you would in Mac OS X. That is, you typically do not manipulate the view-related properties of a UIWindow object directly.

EDIT:

the UIView is something generic (provides common methods you use to create all types of views and access their properties. ) while UIWindow is more concrete (class defines objects that manage and coordinate the windows an application displays on the screen.)

I know is a little vague and I think only apple would know the exact reason for this hierarchy.

http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html#//apple_ref/occ/cl/UIWindow

The UIWindow class defines objects (known as windows ) that manage and coordinate the windows an application displays on the screen. The two principal functions of a window are to provide an area for displaying its views and to distribute events to the views.

and
http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/cl/UIView

The UIView class provides common methods you use to create all types of views and access their properties. For example, unless a subclass has its own designated initializer, you use the initWithFrame: method to create a view. The frame property specifies the origin and size of a view in superview coordinates. The origin of the coordinate system for all views is in the upper-left corner.

UIView objects are arranged within an UIWindow object, in a nested hierarchy of subviews. Parent objects in the view hierarchy are called superviews, and children are called subviews. A view object claims a rectangular region of its enclosing superview, is responsible for all drawing within that region, and is eligible to receive events occurring in it as well. Sibling views are able to overlap without any issues, allowing complex view placement.

只涨不跌 2024-07-26 05:18:38

因为这样每个 UIView 都会具有仅对窗口有意义的(继承的)属性和行为。 那是错误的。

另一种方式更有意义:窗口在视图之上添加行为。 因此它可以绘制、有边界、包含其他视图等。但它通过例如知道如何渲染到显示器来扩展这一点。

在 MacOS X 中,NSWindows 不是视图。 它们包含一个“根视图”,称为 contentView。 iOS 窗口是 NSWindow + 其根视图的组合。

Because then every UIView would have (inherited) properties and behavior that only makes sense for windows. That's just wrong.

The other way around makes more sense: a window adds behavior on top of views. So it can draw, have bounds, contain other views, etc. But it extends this with, e.g., knowing how to render to the display.

In MacOS X, NSWindows are not views. They contain a "root view", called the contentView. iOS windows are a composition of NSWindow + its root view.

缘字诀 2024-07-26 05:18:38

每个常规 UIView 包括主窗口的根视图都应该有一个父视图。 UIWindow 继承 UIView 来执行视图层次结构中父视图的职责。
UIWindow 不是常规视图,它有自己的 UIView 接口方法实现。

Each regular UIView including the main window’s root view should have a parent view. UIWindow inherits UIView to perform duties of a parent view in a view hierarchy.
UIWindow is not regular view and has its own implementations of methods from UIView's interface.

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