如何在导航控制器中的segues之间移动?

时间:2015-08-18 10:19:44

标签: ios objective-c uinavigationcontroller storyboard

在我的故事板中,我有:

NavController-> View1(root) - > View2 - > View3 - > View4

我通常可以按顺序使用流程,并且segues之间的导航很容易。

但是如何从View1转到View4 ..而从View4有后退按钮应该带你到View3? 因为如果我从View1直接Segue到View4,我就无法访问View3。

请让我知道如何做到这一点。提前谢谢。

2 个答案:

答案 0 :(得分:1)

以下是仅使用3个viewcontrollers的示例,但它将是相同的逻辑:

您需要在viewcontrollers之间创建segue,如下所示:

  • viewController1 --- segueFrom1To2 ----- viewController2
  • viewController1 --- segueFrom1To3 ----- viewController3
  • viewController2 --- segueFrom2To3 ----- viewController3

然后你需要在viewController2中使用一个属性,让我们说:goToNextVc; 因此,在viewController1中,您可以在执行适当的segue时设置此属性:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    ViewController2 *vc2 = segue.destinationViewController;

    if ([segue.identifier isEqualToString:@"segueFrom1To3"])
    {
        vc2.goToNextVc = YES;
    }
    else{
        vc2.goToNextVc = NO;
    }
}

现在在viewController2的viewWillAppear中,只需添加:

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    if (self.goToNextVc){
        self.goToNextVc = NO;
        [self performSegueWithIdentifier:@"segueFrom2To3" sender:self];
    }
}

如果您不想看到viewController1和viewController2之间的转换,只需创建一个自定义segue,让我们说SegueNoTransition,您需要在其中覆盖perform:< / p>

-(void) perform{
    [[[self sourceViewController] navigationController] pushViewController:[self destinationViewController] animated:NO];
}

并更换类型&#34; show&#34; of segueFrom1To3 与&#34; custom&#34;以及SegueNoTransition;

的班级

答案 1 :(得分:0)

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

                    if ([controller isKindOfClass:[yourViewController class]]) {
                        [self.navigationController popToViewController:controller
                                                              animated:YES];
                    }
                    else {
                         NSLog(@"Not Found");
                    }
}                
    // You must import the viewController where you want to move.

    // yourViewController = viewController where you want to go in example.