呈现的appdelegate多于一个modalview(present more than one

2019-07-30 17:14发布

我想每一个推送消息后,呈现modalviewcontroller在应用临危“应用程序:(UIApplication的*)应用程序didReceiveRemoteNotification:(NSDictionary的*)USERINFO”

我现在的这个样子的视图 - 控制:

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

所以,当另一个推邮件到达和老ModalViewController还是都看得到,我想提出在老新modalviewcontroller。 但是,这是行不通的。 什么都没有发生,控制台只是说(我认为这是iOS 6的测试版的调试消息):

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

我究竟做错了什么?

PS:我不想解雇老的ViewController,我希望他们能够叠加。

谢谢!

Answer 1:

你可以让你的视图控制器的顶部,然后从顶部视图控制器提出了一种新模式

- (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];
}

您可以调用此方法与RootViewController的是窗口的RootViewController的



Answer 2:

全体面接近,但有这样会使你在某些情况下返回错误的视图控制器犯了一些错误。 这里是一个修正版本。

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
}


Answer 3:

下面是与上面相同,但写在斯威夫特

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
}


文章来源: present more than one modalview in appdelegate