【问题标题】:How to navigate to UIViewController which is not present in navigationStack如何导航到 navigationStack 中不存在的 UIViewController
【发布时间】:2019-01-07 11:08:42
【问题描述】:

我有三个 UIViewController。我正在从 VC1 导航到 VC3。如果我点击 VC3 中的取消或完成按钮,我想导航到 VC2。

我以编程方式将 VC2 控制器添加到导航堆栈。

    DocListViewController *temp1 = [[DocListViewController alloc] initWithNibName:@"DocListViewController" bundle:nil];
    [self.navigationController addChildViewController: temp1];

    for (UIViewController *controllers in  self.navigationController.viewControllers) {

      if([controllers isKindOfClass: [DocListViewController class]]) {            

        DocListViewController *VC2ViewController = (DocListViewController*)controllers;
        [self.navigationController popToViewController:VC2ViewController animated:YES];            
      }
    }

【问题讨论】:

  • 现在发生了什么事?你在哪里打电话给addChildViewController?应该在推送 VC3 之前调用它
  • @Lavanya v gowda 在 VC1 中没有动画的情况下在 VC3 之前推送 VC2,并且只在 VC1 中推送带有动画的 VC3。
  • Pratik Sodha 你能告诉我怎么做吗?
  • 为什么不使用 pushViewController
  • 只需简单地从 VC1 中 psuh VC2 和 VC3。但是VC2推送没有动画。因此,您可以实现您的确切要求。

标签: ios objective-c uinavigationcontroller


【解决方案1】:

您在此处做错的是将其添加为子视图控制器,addChildViewController 因为它作为当前视图控制器的子项不会将其添加到导航堆栈中。

在执行popToViewcontroller 之前,您需要在导航堆栈中插入控制器。

VC2 *temp1 = [[VC2 alloc] initWithNibName:@"DocListViewController" bundle:nil];
NSMutableArray *vcArray = [NSMutableArray arrayWithArray:self.navigationController.viewControllers] ;
[vcArray insertObject:temp1 atIndex:1]; // ** This index is `1` assuming you only have 2 controllers and we are pushing it in the middle,
// if you have many vc in navigation stack and just want to insert a new vc just before your current vc go with this:
/*
[vcArray insertObject:temp1 atIndex:vcArray.count - 2];
*/

self.navigationController.viewControllers = vcArray;
/* --- ** This part is also not needed:
  for (UIViewController *controllers in  self.navigationController.viewControllers) {
    if([controllers isKindOfClass: [DocListViewController class]]) {                   
       [self.navigationController popToViewController: controllers animated:YES];            
    }
}
*/
[self.navigationController popViewControllerAnimated:true];

另外:你不需要VC2 *VC2ViewController = (VC2*)controllers;if 块内,因为您只需要一个UIViewController 类型的对象即可弹出。

我刚刚编辑了你的代码,获取下面的项目参考

编辑: 添加 GitHub 链接以获得更好的参考: PlayingWithNavigation

【讨论】:

  • 我遇到了异常:1. 不鼓励在分离的视图控制器上显示视图控制器 。 2. popToViewController:transition:在发生现有转换或演示时在 上调用;导航堆栈不会更新。
  • 完美运行,我将分享我创建的项目的 GitHub 链接。
  • 你可以查看这个:github.com/subhajitregor/PlayingWithNavigation我已经上传了一个使用上述代码和3个控制器的简约演示代码
  • 在此之后如果您遇到任何问题,请告诉我
  • VC2 和 VC3 控制器在 PlayingWithNavigation 项目的情节提要中不存在。
【解决方案2】:

您可以使用 pushviewContoller 导航到 VC2

NSString * storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
VC2 * VC2View = [storyboard instantiateViewControllerWithIdentifier:@"VC2ID"]; // "VC2ID" is the storyboard Id of your view controller
[self.navigationController pushViewController:VC2View animated:YES];

【讨论】:

    【解决方案3】:
        UIViewController *vc2 = [[UIViewController alloc] init];
        UIViewController *vc3 = [[UIViewController alloc] init];
    
        [self.navigationController pushViewController:vc2 animated:NO];
        [self.navigationController pushViewController:vc3 animated:YES];
    

    试试这个。

    【讨论】:

      猜你喜欢
      • 2022-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-19
      • 2012-06-11
      • 1970-01-01
      相关资源
      最近更新 更多