Present more than one modal view in appdelegate

I want to introduce a modalviewcontroller after every push message that the application receives in the application "application: (UIApplication *) didReceiveRemoteNotification: (NSDictionary *) userInfo"

I present the viewcontroller as follows:

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 visible, I want to introduce a new modalviewcontroller on top of the old one. But that will not work. Nothing happened and the console just says (I think this is a debug message from 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 do not want to reject the old ViewController, I want them to be a stack.

Thanks!

+6
source share
3 answers

You can get the top of your view controllers and then introduce a new modal from this top level 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 using rootViewController - this is the rootViewController window

+11
source

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 } 
0
source

Full Decent was close, but had a few errors that cause you to return the wrong view controller in some cases. Here is the revised 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 } 
0
source

Source: https://habr.com/ru/post/921802/


All Articles