UIWindow UIView addSubview问题

发布于 2024-12-06 21:22:25 字数 566 浏览 1 评论 0原文

我读过的每个视频教程和书籍都显示以下代码,用于将 UIView 添加到 UIWindow。

[窗口addSubview:self.viewController.view];

我对上面代码的理解是,将一个“View”(这是 UIView 的实例)添加到窗口(这是 UIWindow 的实例)。让我分解一下(根据我的理解):

窗口(UIWindow) addSubview(将视图添加到窗口的方法) Self.viewController.view (简单地返回一个已在 UIViewController 类中实例化的“视图”实例。

我遇到的第一个问题是我在苹果网站上的 UIWindow 类参考文档中找不到方法“addSubview”。然而,有人好心地向我指出,UIWindow 继承了 UIView 的 addsubview 方法,这很好,但是为什么所有书籍和在线文档都说 addsubview 方法向窗口添加了一个视图 - 但这到底是怎么回事呢?有人可以逐步解释一下这段代码在做什么吗?如果 UIWindow 继承了 UIView 的 addsubview 方法,那么它如何返回继承树呢?我真正需要的是带有图表的小示例代码。一步一步发生真的非常感谢。

Every video tutorial and book I have read displays the following code to add a UIView to the UIWindow.

[window addSubview:self.viewController.view];

My understanding of the above code is that a "View" (which is an instance of a UIView) is added to the window (Which is an instance of UIWindow). Let me break it down (According to my understanding):

window (UIWindow)
addSubview (method to add a View to a window)
Self.viewController.view (simply returns an instance of a "view" which is already instantiated within the UIViewController class.

The first problem I have is that I could not find the method "addSubview" in the UIWindow class reference document on apples site. However somebody kindly pointed out to me that UIWindow inherits addsubview method from UIView. thats all fine, but why do all the book and online documents state that the addsubview method adds a view to the window - but how can that be? really confused. Can somebody please explain step by step what this code is doing? If UIWindow inherits the addsubview method of UIView then how can it go back up the inheritance tree? really lost. What I really need is small example code with diagrams of what is happening step by step. would be REALLY greatfull. many thanks

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

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

发布评论

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

评论(3

风吹雨成花 2024-12-13 21:22:25

将窗口视为与屏幕或绘图对象直接关联的视图。

在上面的示例中,window.view 不正确。窗口不包含视图,它是具有附加行为的视图。

假设您正在从 NIB 文件加载 UIViewController,则与 viewController 关联的视图将通过访问该视图来实例化。所以...

您可能会看到像

MyViewController *vc = [MyViewController alloc]initWithNibName:@"MyNibFile" bundle:nil]autorelease];
[window addSubView:vc.view];
[window makeKeyAndVisible];

View 这样的代码只是 Window 的超类,因此您可以使用任何公共视图方法。

通常,AppDelegate 对象中的窗口是在加载 MainWindow.xib 文件时实例化的。

您应该

@property(nonatomic, retain) IBOutlet UIWindow *window;

在 AppDelegate 头文件中看到类似的内容。 (IBOutlet 指令告诉在加载 nib 文件时初始化窗口对象。

请记住,UIWindow 只是一个具有附加行为和数据的 UIView。

希望这会有所帮助。

Think of a window as a view that's associated directly with a screen or drawing object.

In the above example window.view is not correct. a window does not contain a view, it is a view with additional behavior.

Assuming that you are loading a UIViewController from a NIB file, the view associated with the viewController will be instantiated by accessing the view. So ...

You might see code like

MyViewController *vc = [MyViewController alloc]initWithNibName:@"MyNibFile" bundle:nil]autorelease];
[window addSubView:vc.view];
[window makeKeyAndVisible];

View is simply a super class of Window so any public view method is available to you.

Generally the window in your AppDelegate object is instantiated when the MainWindow.xib file is loaded.

You should see something like

@property(nonatomic, retain) IBOutlet UIWindow *window;

in your AppDelegate header file . (The IBOutlet directive tells the initialize the window object when the nib file is loaded.

Just remember, a UIWindow is simply a UIView with additional behaviors and data.

Hope this helps.

走走停停 2024-12-13 21:22:25

“然而,有人好心地向我指出,UIWindow 继承了 UIView 的 addsubview 方法。这很好,但为什么所有书籍和在线文档都说 addsubview 方法向窗口添加了一个视图 - 但这怎么可能呢?真的很困惑。有人可以逐步解释一下这段代码在做什么吗?如果 UIWindow 继承了 UIView 的 addsubview 方法,那么它如何返回继承树?”

就是这样。我认为你不明白什么是继承。比喻是“是”。 UIWindow“是”UIView。它拥有 UIView 所拥有的一切,甚至更多。 UIView 拥有的一件事是添加子视图的能力。因此 UIWindow 也具有这种能力。它不需要任何其他 UIView 来完成它。它是一个 UIView。它自己可以做到。

"However somebody kindly pointed out to me that UIWindow inherits addsubview method from UIView. thats all fine, but why do all the book and online documents state that the addsubview method adds a view to the window - but how can that be? really confused. Can somebody please explain step by step what this code is doing? If UIWindow inherits the addsubview method of UIView then how can it go back up the inheritance tree?"

That's it. I think you are not understanding what inheritance is. The metaphor is "is a". A UIWindow "is a" UIView. It has everything a UIView has, and more. One thing a UIView has is the ability to addSubview. Therefore a UIWindow has that ability too. It doesn't need any other UIView to do it for it. It is a UIView. It can do it itself.

╰◇生如夏花灿烂 2024-12-13 21:22:25

尝试一下

[window.view addSubview:self.viewController.view];

这不是我的想法,所以它可能不完全准确。

try

[window.view addSubview:self.viewController.view];

That is off the top of my head, so it may not be completely accurate.

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