为什么UIWindow是一个孩子,也是UView的父母?

时间:2022-10-19 21:55:22

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.

UIKit参考说UIView是UIWindow的超类,但是尽管有这种父母,UIWindow实际上管理UIViews。这对我来说听起来很不寻常。

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.

编辑:我阅读了iPhone编程指南中的相关段落。然而我无法理解为什么他们会这样做:让UIWindow成为UIView的父母。肯定有一些事情迫使Apple以这种方式设计类层次结构。

3 个解决方案

#1


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.

有经验的Mac OS X开发人员可能会发现UIWindow类的不寻常之处在于它的继承。在Mac OS X中,NSWindow的父类是NSResponder。在iPhone OS中,UIWindow的父类是UIView。因此,在iPhone OS中,窗口也是视图对象。尽管它具有亲子关系,但您通常会将iPhone OS中的窗口视为与Mac OS X中的窗口相同。也就是说,您通常不直接操纵UIWindow对象的视图相关属性。

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.)

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

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.

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

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类提供了用于创建所有类型的视图并访问其属性的常用方法。例如,除非子类具有自己的指定初始值设定项,否则使用initWithFrame:方法创建视图。 frame属性指定superview坐标中视图的原点和大小。所有视图的坐标系原点都在左上角。

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.

UIView对象排列在UIWindow对象中,位于子视图的嵌套层次结构中。视图层次结构中的父对象称为超级视图,子级称为子视图。视图对象声称其封闭的超视图的矩形区域,负责该区域内的所有绘图,并且还有资格接收其中发生的事件。同级视图可以重叠而不会出现任何问题,从而允许复杂的视图放置。

#2


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

因为那时每个UIView都有(继承的)属性和行为只对windows有意义。那是错的。

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.

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

#3


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.

每个常规UIView(包括主窗口的根视图)都应具有父视图。 UIWindow继承UIView以在视图层次结构中执行父视图的职责。 UIWindow不是常规视图,并且有自己的UIView接口方法实现。

#1


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.

有经验的Mac OS X开发人员可能会发现UIWindow类的不寻常之处在于它的继承。在Mac OS X中,NSWindow的父类是NSResponder。在iPhone OS中,UIWindow的父类是UIView。因此,在iPhone OS中,窗口也是视图对象。尽管它具有亲子关系,但您通常会将iPhone OS中的窗口视为与Mac OS X中的窗口相同。也就是说,您通常不直接操纵UIWindow对象的视图相关属性。

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.)

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

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.

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

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类提供了用于创建所有类型的视图并访问其属性的常用方法。例如,除非子类具有自己的指定初始值设定项,否则使用initWithFrame:方法创建视图。 frame属性指定superview坐标中视图的原点和大小。所有视图的坐标系原点都在左上角。

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.

UIView对象排列在UIWindow对象中,位于子视图的嵌套层次结构中。视图层次结构中的父对象称为超级视图,子级称为子视图。视图对象声称其封闭的超视图的矩形区域,负责该区域内的所有绘图,并且还有资格接收其中发生的事件。同级视图可以重叠而不会出现任何问题,从而允许复杂的视图放置。

#2


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

因为那时每个UIView都有(继承的)属性和行为只对windows有意义。那是错的。

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.

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

#3


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.

每个常规UIView(包括主窗口的根视图)都应具有父视图。 UIWindow继承UIView以在视图层次结构中执行父视图的职责。 UIWindow不是常规视图,并且有自己的UIView接口方法实现。