Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 960
CrEaK Avatar asked Jul 31 '12 20:07

CrEaK


2 Answers

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

like image 83
Hoàng Toản Avatar answered Oct 02 '22 17:10

Hoàng Toản


Full Decent was close, but had a few mistakes that cause you to return the wrong view controller in some cases. Here is a corrected version.

private func topViewController(rootViewController: UIViewController) -> UIViewController {
    var rootViewController = UIApplication.sharedApplication().keyWindow!.rootViewController!
    repeat {
        guard let presentedViewController = rootViewController.presentedViewController else {
            return rootViewController
        }

        if let navigationController = rootViewController.presentedViewController as? UINavigationController {
            rootViewController = navigationController.topViewController ?? navigationController

        } else {
            rootViewController = presentedViewController
        }
    } while true
}
like image 36
AnthonyMDev Avatar answered Oct 02 '22 16:10

AnthonyMDev