Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

ios - What is the difference between UIApplication.sharedApplication.delegate.window and UIApplication.sharedApplication.keyWindow?

Can anyone help me understand the difference between the following two lines:

[UIApplication.sharedApplication.delegate.window addSubview:myView];
[UIApplication.sharedApplication.keyWindow addSubview:myView];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...