【问题标题】:present more than one modalview in appdelegate在 appdelegate 中呈现多个 modalview
【发布时间】:2012-07-31 20:53:57
【问题描述】:

我想在应用程序在“application:(UIApplication *)application 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,我希望它们堆叠起来。

谢谢!

【问题讨论】:

    标签: iphone modalviewcontroller uiapplicationdelegate


    【解决方案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

    【讨论】:

      【解决方案2】:

      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
      }
      

      【讨论】:

        【解决方案3】:

        这里和上面一样,但是是用 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
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-02-27
          • 1970-01-01
          • 2013-08-29
          • 1970-01-01
          • 2015-01-13
          • 1970-01-01
          • 2015-11-14
          相关资源
          最近更新 更多