how to access "UIWindow" from any class?

Discussion in 'Mac Programming' started by springframework, Apr 1, 2008.

  1. springframework macrumors member

    Mar 31, 2008
    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?
  2. springframework thread starter macrumors member

    Mar 31, 2008
    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?
  3. Littleodie914 macrumors 68000


    Jun 9, 2004
    Rochester, NY
    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

Share This Page