打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
diffrence between [[[[UIApplication sharedApplication] delegate] window] and [[UIApplication sharedA

Hello i am newbie in iPhone development can any one help me to know difference between following two lines:

First: 

[[[[UIApplication sharedApplication] delegate] window] addSubview:UIView];

Second:

[[UIApplication sharedApplication].keyWindow addSubview:UIView];

Thanks in advance.

shareimprove this question

They are the same in iOS, but on Mac OS they can be different, because in iOS there is only one window, while on Mac OS could be more than one. 
from the documentation:
- For [[[UIApplication sharedApplication] delegate] window];

The window to use when presenting a storyboard. This property contains the window used to present the app’s visual content on the device’s main screen.

i.e this is the property window you have in your appDelegate.h file

  • for [[UIApplication sharedApplication] keyWindow];

This property holds the UIWindow object in the windows array that is most recently sent the makeKeyAndVisible message.

in iOS you sent makeKeyAndVisible in your appDelegate.m inside 
application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
to window, which what we have above, so they are the same. (This is done automatically if you are using storyboards)

shareimprove this answer

For most uses, they will be the same… but not always.

[UIApplication sharedApplication].keyWindow is the window which is currently being displayed on the device. This is normally your application's window, but could be a system window.

[UIApplication sharedApplication].delegate.window is the window your application is expected to use.

Which one should be used? Well that all depends on context.

If you are updating part of your application, then you should add views to your application's window. This is almost always what you want to do.

Personally, I always use [[UIApplication sharedApplication].delegate.window addSubview:view]or [self.view.window addSubView:view] (within UIViewController) when I need to add a view directly to the window.

There might be some times when you want to present a view to the window currently being displayed, regardless if the window belongs to your application or is some system window. I've not run into that situation.

shareimprove this answer

The simplest setup is to just have one UIWindow. Usually that window is kept as a property on your app delegate. The keyWindow is the one that is designated to receive keyboard and other non-touch related events. Only one window at a time may be the key window. So if you add a 2nd window and make it the keyWindow (via [window makeKeyAndVisible]), your lines return different windows!

shareimprove this answer
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
iphone下,如何获取对象的句柄和其父对象的句柄?
Unity与iOS相互调起、交互
3、show—UIWindow简单介绍
Ios开发中UILocalNotification实现本地通知实现提醒功能
ios 如何隐藏状态栏
IOS获取顶层UIViewController
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服