1

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!

Georgi
  • 674
  • 7
  • 21
  • Currently I saved the rootController as a variable before presenting the UIActionSheet, but I think there should be a better way. – Georgi Nov 08 '13 at 15:33
  • Doesn't your app delegate have a `window` property? That would be the main window of the app. – rmaddy Nov 08 '13 at 15:33

3 Answers3

2

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.

Vitaly
  • 515
  • 3
  • 9
  • 1
    BTW, `[[UIApplication sharedApplication] keyWindow]` works fine in the iOS < 7. – Vitaly Nov 09 '13 at 00:48
  • Going through the windows and finding the one with `UIWindowLevelNormal` works as it should, thanks a lot! :) – Georgi Nov 11 '13 at 09:45
0

You can reach window by using this code:

UIWindow* window = [[UIApplication sharedApplication] keyWindow];
  • Did you read the question? The OP states that the `keyWindow` is giving the action sheet window. That's kind of the whole point to the question. – rmaddy Nov 08 '13 at 19:21
0

Eric's answer here is the most reliable solution. Note that you can't add/animate a view while action sheet is present. And without reference to the actual action sheet to set your controller as the delegate and know when the sheet is dismissed, you don't know when action sheet is dismissed. This is just an FYI, as those are the problems I'm currently running into myself.

Community
  • 1
  • 1
Alex the Ukrainian
  • 4,528
  • 3
  • 24
  • 25