I have a network operation running in the background that might need to trigger a modal view, depending on the response from the server.
This network operation can be started at several different points in the app and can't always know ahead of time which UIViewController will be active when the server response comes back.
Anyway, I came up with a solution for my purposes. It turns out that you don't need to be the *top* UIVewController in order to show a modal view. Apparently, any controller in the hierarchy can do it.
Also, it turned out it was my own fault that
[[[UIApplication sharedApplication] keyWindow] rootViewController] was returning a nil value. Typically in the
application:didFinishLaunchingWithOptions: method of the UIApplicationDelegate there's a line like this
Code:
self.window.rootViewController = self.viewController;
For some reason I can no longer remember, I had changed this to something like
Code:
[self.window addSubview:self.viewController.view];
When I changed this back to standard practice, I was able to present a modal view from any object with
Code:
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentModalViewController:controller animated:YES];