【问题标题】:Need to return to the presenting UIViewController from within a presented UINavigationController需要从呈现的 UINavigationController 中返回呈现的 UIViewController
【发布时间】:2014-07-02 18:12:57
【问题描述】:

在 MyMainViewController 中,我展示了一个这样的导航控制器:

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
UINavigationController* nc = [storyboard instantiateViewControllerWithIdentifier:@"NAVIGATION_CONTROLLER_ID"];
[self presentViewController:nc animated:YES completion:nil];

稍后,我需要从 UINavigationController 的视图层次结构中的某个位置返回到 MyMainViewController。我该怎么做?

(注意:MyMainViewController 是在 .XIB 中定义的,而不是在定义 UINavigationController 及其子项的情节提要中。)

【问题讨论】:

    标签: ios uiviewcontroller uinavigationcontroller xib uistoryboard


    【解决方案1】:

    听起来您已经以模态方式呈现了一个要删除的 NavController。模态呈现的 VC 可以自行删除。

    在你的 NavController 中添加:

    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"Dismissed nav controller modally");
    }]
    

    【讨论】:

      【解决方案2】:
      [self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:2] animated:YES];
      

      我猜你知道你的视图控制器的索引。如果您只是想返回rootViewController,您可以这样做

      [self.navigationController popToRootViewControllerAnimated:YES];
      

      如果您想将新的 viewController 推送到导航堆栈,只需这样做

      MyMainViewController *mainController = [[MyMainViewController alloc] initWithNibName:@"MyMainViewController" bundle:nil];
      [self.navigationController pushViewController:desController animated:YES];
      

      回到之前的 viewController 会是

      [self dismissViewControllerAnimated:YES completion:nil];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多