present more than one modalview in appdelegate

让人想犯罪 __ 提交于 2019-12-04 12:21:05

问题


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!


回答1:


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




回答2:


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
}



回答3:


Here is the same as above, but written in Swift

private func topViewController() -> UIViewController {
    var rootViewController = UIApplication.sharedApplication().keyWindow!.rootViewController!
    repeat {
        if rootViewController.presentingViewController == nil {
            return rootViewController
        }
        if let navigationController = rootViewController.presentedViewController as? UINavigationController {
            rootViewController = navigationController.viewControllers.last!
        }
        rootViewController = rootViewController.presentedViewController!
    } while true
}


来源:https://stackoverflow.com/questions/11748845/present-more-than-one-modalview-in-appdelegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!