本文介绍了UIWindow UIView addSubview问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读过的所有视频教程和书籍都显示以下代码,以将UIView添加到UIWindow.

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

[window addSubview:self.viewController.view];

[window addSubview:self.viewController.view];

我对上述代码的理解是,在窗口中添加了视图"(这是UIView的一个实例)(这是UIWindow的一个实例).让我分解一下(根据我的理解):

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

窗口(UIWindow)addSubview(将视图添加到窗口的方法)Self.viewController.view(仅返回已在UIViewController类中实例化的视图"的实例.

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.

我遇到的第一个问题是,我在apples网站上的UIWindow类参考文档中找不到方法"addSubview".但是有人向我指出,UIWindow从UIView继承了addsubview方法.没关系,但是为什么所有的书籍和在线文档都声明addsubview方法将视图添加到窗口中-怎么可能呢?真的很困惑.有人可以逐步解释此代码的作用吗?如果UIWindow继承了UIView的addsubview方法,那么如何将其备份到继承树上?真的迷路了我真正需要的是带有示例代码的小示例代码,这些代码逐步说明了所发生的事情.真的很棒.非常感谢

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

推荐答案

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

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

在上面的示例window.view是不正确的.窗口不包含视图,而是具有其他行为的视图.

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

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

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];

视图只是Window的超类,因此您可以使用任何公共视图方法.

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

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

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;

在您的AppDelegate头文件中.(当装入nib文件时,IBOutlet指令告诉初始化窗口对象.

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

请记住,UIWindow只是具有其他行为和数据的UIView.

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

希望这会有所帮助.

这篇关于UIWindow UIView addSubview问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:41