6

我想在应用程序在“应用程序:(UIApplication *)应用程序didReceiveRemoteNotification:(NSDictionary *)userInfo”中收到的每个推送消息之后呈现一个modalviewcontroller

我这样呈现视图控制器:

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

因此,当另一个推送消息到达并且旧的 ModalViewController 仍然可见时,我想在旧的 modalviewController 上展示一个新的 modalviewcontroller。但它不起作用。什么也没发生,控制台只是说(我认为这是 iOS 6 Beta 的调试消息):

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

我究竟做错了什么?

PS:我不想关闭旧的 ViewController,我希望它们堆叠起来。

谢谢!

4

3 回答 3

11

您可以获取视图控制器的顶部,然后从该顶部视图控制器呈现一个新模式

- (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

于 2012-07-31T21:04:32.913 回答
1

Full Decent 很接近,但有一些错误导致您在某些情况下返回错误的视图控制器。这是一个更正的版本。

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
}
于 2016-02-04T19:20:30.917 回答
0

这里和上面一样,但是是用 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
}
于 2015-12-21T09:24:58.710 回答