Question

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!

Was it helpful?

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.

OTHER TIPS

You can reach window by using this code:

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top