View Full Version : how to access "UIWindow" from any class?

Apr 1, 2008, 03:21 PM
I know there is only one UIWindow object so there should be something like UIWindowInstance(); to retrieve it, allowing you to add views and get rid of views in whatever class you want.

so what is the way to get the UIWindow?

Apr 1, 2008, 04:36 PM
Inside a class that extends UIView i can use self.window to get the window.

the instance of this class that i call self.window from is already added to the window. I am not sure if this would work before the instance is added to the window.

does anyone have a way of accessing the UIWindow from any class, without passing a reference to it or a reference to the class that created it?

I tried to make it a global variable but this didn't seem to work for me, maybe I just did it wrong?

Apr 1, 2008, 05:29 PM
Sure! :)

There are two different ways to access the UIWindow objects currently being used by the application:

[[UIApplication sharedApplication] keyWindow]

This will return the currently "on top" window, (technically, the one that was most recently told to display itself) and is probably the only one you'll need for a simple application.

For anything more elaborate though, especially if you know you'll be using more than one window, you can use:

[[UIApplication sharedApplication] windows]

Which will return an NSArray containing all the UIWindow objects currently "owned" by the application. Happy coding! :D