Bharat Bhushan
Bharat Bhushan

Reputation: 419

Is it possible that an iOS application can have more than one window?

I have been asked this question many times in the interview searched every where didn't get any proper answer.So finally posting this question here.

Upvotes: 24

Views: 10202

Answers (3)

nilam_mande
nilam_mande

Reputation: 931

Generally one application require only 1 UIWindow, but still there may be some scenarios where you need to use multiple UIWindow in one application.

For example, you wish to show a view on the top of system AlertViews, or can the default Keyboard.

UIWindows are the special UIViews, for which their display order is controlled by .windowLevel property.

You don't need to add a new UIWindow as a subview of any of view. You can simply create a new UIWindow and call either window setHidden:NO or window makeKeyAndVisible depend on the level, you have given to it.

There are three default window enum levels defined:

  • UIWindowLevelNormal
  • UIWindowLevelStatusBar
  • UIWindowLevelAlert

Upvotes: 7

Karthik Kumar
Karthik Kumar

Reputation: 1385

You may go through this.

Yes, you can have multiple windows. A key window is the one that receives the user input.

Starting with Rob's answer I played around a bit and would like to write down some notes for others trying to get information on this topic:

  1. It is not a problem at all to add another UIWindow. Just create one and makeKeyAndVisible. Done.
  2. Remove it by making another window visible, then release the one you don't need anymore.
  3. The window that is "key" receives all the keyboard input.
  4. UIWindow covers everything, even modals, popovers, etc. Brilliant!
  5. UIWindow is always in portrait implicitly. It does not rotate.
  6. You'll have to add a controller to the new window's root controller and let that handle rotation.
  7. (Just like the main window) The window's level determines how "high" it gets displayed. Set it to UIWindowLevelStatusBar to have it cover everything.
  8. Set its hidden property to NO. A 2nd UIWindow can be used to bring views on the screen that float on top of everything. Without creating a dummy controller just to embed that in a UIPopoverController.
  9. It can be especially useful for iPhone where there is no popover controller but where you might want to mimic something like it.
  10. And yes, it solved, of course, my problem: if the app resigns activation, add a cover window over whatever is currently shown to prevent iOS from taking a screenshot of your app's current content.

Upvotes: 51

Fahri Azimov
Fahri Azimov

Reputation: 11770

Of course it can have multiple windows. Just, only one to be displayed at a time, that's the keyWindow. You can have multiple windows stored in array or whatsoever, and make them keyWindow when you want to display them.

And, yeah, read the documentation @Mannopson suggested, it's very useful.

Upvotes: -1

Related Questions