Search code examples
iosuiactionsheetuiwindow

Get the UIWindow which is under the UIActionSheet


I am trying to present a new UIViewController on top of the current current UIViewController. Here is the line of code that creates the desired effect:

LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
UIViewController *rootController = [[[UIApplication sharedApplication] keyWindow] rootViewController];

[rootController presentViewController:loginViewController animated:YES completion:nil];

The problem is that I added an UIActionSheet which asks the user to confirm the desired action (which is to log out). When the user confirms the action I run the above mentioned peace of code, but the UIActionSheet is still the keyWindow. Therefore the LoginViewController is not presented on top (the rootController is null when I try to debug).

My question is: Can I somehow find the UIWindow which is under the UIActionSheet and from there get the root controller,or maybe I can dismiss programmatically the UIActionSheet when the users selects the log out action and only then execute the above code?

Thank you in advance!


Solution

  • Just got same issue and have found a workaround. You can just go through all application windows and get a window with windowLevel equals UIWindowLevelNormal.

    Sure, this will work correctly only if you have one normal window in your app. If you have more than one you should have your own rule how to detect the correct window.

    Hope this will help.