Search code examples
iphonemodalviewcontrolleruiapplicationdelegate

present more than one modalview in appdelegate


I want to present a modalviewcontroller after every push-message the app recieves in "application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo"

I present the viewcontroller like this:

ReleaseViewController *viewController = [[ReleaseViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.window.rootViewController presentModalViewController:navController animated:YES];

So when another push-message arrives and the old ModalViewController is still visibile, I want to present a new modalviewcontroller over the old. But it doesn't work. Nothing happened and the console just says (I think it's a debug-message of iOS 6 Beta):

Warning: Attempt to present <UINavigationController: 0x1dde6c30> on <UINavigationController: 0x1dd73c00> whose view is not in the window hierarchy!

What am I doing wrong?

PS: I don't want to dismiss the old ViewController, I want them to stack.

Thanks!


Solution

  • You can get top of your view controllers, then present a new modal from that top view controller

    - (UIViewController *)topViewController:(UIViewController *)rootViewController
    {
        if (rootViewController.presentedViewController == nil) {
            return rootViewController;
        }
    
        if ([rootViewController.presentedViewController isMemberOfClass:[UINavigationController class]]) {
            UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
            UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
            return [self topViewController:lastViewController];
        }
    
        UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
        return [self topViewController:presentedViewController];
    }
    

    You can call this method with rootViewController is window's rootViewController