views:

117

answers:

3

Hello

I have only one window and I tried

UIWindow* mWindow = [[UIApplication sharedApplication] keyWindow];

but this returned nil.

I also tried:

UIWindow* mWindow = (UIWindow*)[[UIApplication sharedApplication].windows objectAtIndex:0];

But this raised an exception and the app closed, when I tried to print out

[[UIApplication sharedApplication].windows count]

It printed 0

Note: I am putting this in my only view controller's viewDidLoad method and this is completely a new iPad View Based Application so I changed nothing, just trying to get the window

Please help me to get this object

A: 

why do you want this object? UIWindow isn't meant to be used as a view or manipulated at all really. Its meant to be a 'container' for views and view controllers and etc. but if you set up a generic application, your app delegate has a ivar named window that is the UIWindow of the application.

Jesse Naugher
I'll tell you why I need it, it's because I inherited UIWindow to build my customized window and I want to get it here to pass some information to it
Aubada Taljo
A: 

If your main window is an outlet of your AppDelegate (which should be the case), you may simply use

MyAppDelegate* myDelegate = (((MyAppDelegate*) [UIApplication sharedApplication].delegate));
[myDelegate.window ...]
Phlibbo
Yes this worked nicely, thanks... :)
Aubada Taljo
+1  A: 

Your application's key window isn't set until [window makeKeyAndVisible] gets called in your app delegate. Your UIViewController is probably being loaded from a NIB before this call. This explains why keyWindow is returning nil.

Luckily, your view controller doesn't need to go through UIApplication to get the window. You can just do:

UIWindow *mWindow = self.view.window;
cduhn
Thank you for your help but this didn't work in viewDidLoad..
Aubada Taljo