UIWindow UIView addSubview issue

╄→尐↘猪︶ㄣ 提交于 2019-12-24 03:32:13

问题


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


回答1:


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.




回答2:


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




回答3:


try

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

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



来源:https://stackoverflow.com/questions/7543036/uiwindow-uiview-addsubview-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!