【问题标题】:dismissing modalViewController of modalViewController关闭 modalViewController 的 modalViewController
【发布时间】:2011-09-16 16:54:34
【问题描述】:

所以我有一个 UITabBarController 应用程序,我想显示一个登录页面,所以我做到了:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidLogin:) name:UserDidLoginNotification object:nil];
LoginViewController* loginViewController = [[LoginViewController alloc] init];
        self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
        [self.tabBarController.selectedViewController presentModalViewController:loginViewController animated:NO];
        [loginViewController release];

在我的 LoginViewController 中,我还可以显示另一个 modalViewController:

- (void) twitterLogin: (UIViewController *) askingView
{
    UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _twitter delegate: self];

    if (controller) {
        self.askingView = askingView;
        [askingView presentModalViewController: controller animated: YES];
    }
}

我有以下方法,其中askingView 是LoginViewController, 当我想解雇这个时,我会这样做:

[self.askingView dismissModalViewControllerAnimated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:UserDidLoginNotification object:nil];

但是,这不会关闭 LoginViewController 并显示 UITabBarController 视图。它只是关闭了从 LoginvVIewController 显示的我的 modalViewController。我在这里做错了什么?我也收到以下错误:

attempt to dismiss modal view controller whose view does not currently appear. self = <LoginViewController: 0x2aff70> modalViewController = <SA_OAuthTwitterController: 0x2d2a80>
2011-09-16 09:45:37.750 VoteBooth[4614:707] attempt to dismiss modal view controller whose view does not currently appear. self = <MainViewController: 0x29fec0> modalViewController = <LoginViewController: 0x2aff70>

【问题讨论】:

    标签: iphone objective-c ipad modalviewcontroller


    【解决方案1】:

    为了关闭显示在另一个模态视图上的模态视图,您必须在父级的父级上调用dismissModalViewControllerAnimated:。我在我的一些应用程序中使用了它,它对我来说效果很好(经过许多艰苦的时间试图弄清楚)。这正是我使用过的:

    [[[self parentViewController] parentViewController] dismissModalViewControllerAnimated:YES];
    

    【讨论】:

    • 这对我来说很好,直到我在我的 iPhone 3GS 上升级到最新的 Xcode 和 iOS 5,有什么想法吗?
    • 尝试将两个“parentViewController”引用更改为“presentingViewController”。这似乎对我有用。
    • 是的,你是对的。现在我先检查哪个IOS,然后再决定做什么。
    【解决方案2】:
    if ([self respondsToSelector:@selector(presentingViewController)]) {
        [self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES]; // for IOS 5+
    } else {
        [self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES]; // for pre IOS 5
    }
    

    【讨论】:

      【解决方案3】:

      如果你有一个动态的 UX 并且不知道要去找多少父母,你可以使用这个递归函数来弄清楚......

      - (void) goHome
      {
          //Dismiss modal back to home page
          int numberOfVcsToDismiss = [self findRootViewController:self];
          [self dismissToRootVc:numberOfVcsToDismiss];
      }
      
      - (int) findRootViewController:(UIViewController*)vc
      {
          if(vc)
          {
              return 1 + [self findRootViewController:vc.presentingViewController];
          }
          return 0;
      }
      
      - (void) dismissToRootVc:(int)count
      {
          if(count == 1)
              [self dismissViewControllerAnimated:YES completion:^{}];
          if(count == 2)
              [self.presentingViewController dismissViewControllerAnimated:YES completion:^{}];
          if(count == 3)
              [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^{}];
          if(count == 4)
              [self.presentingViewController.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^{}];
          if(count == 5)
              [self.presentingViewController.presentingViewController.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^{}];
          //etc....
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-10
        • 2012-04-19
        相关资源
        最近更新 更多