Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get my UIWindow using UIApplication?

People also ask

What is Uiwindow in Swift?

The backdrop for your app's user interface and the object that dispatches events to your views.

How do you hide Uiwindow?

The correct way to hide a window is to set the hidden property to YES. To remove it from UIApplication's windows property you just release the window (in ARC you set all references to nil). Of course you would want to have another window in place at this time.


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 ...]

Easiest way is to get the window from the app delegate instead:

UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
// Do something with the window now

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;

[[[UIApplication sharedApplication] windows] objectAtIndex:0]; // You can also check the count of this to make sure, because if there are no windows it will crash.