Pop to root then perform segue on iPhone

◇◆丶佛笑我妖孽 提交于 2019-12-11 04:59:38

问题


I am running on iOS 6.0 storyboard enabled

I have a NavController linked to a TableViewController. This TableView can segue to AViewController or BViewController.

When I am in A, I want to pop back to the root and perform segue to B with this line :

UINavigationController *nav = self.navigationController;
[nav popToRootViewControllerAnimated:YES];
[nav performSegueWithIdentifier:@"GoToB" sender:self];

I checked the storyboard, GoToB do exist and is linked from the TableViewController to BViewController

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<NavMainViewController: 0xb921fa0>) has no segue with identifier 'GoToB''

What am I missing ?


回答1:


The segue will be attached to the view controller you pop to, not nav which is the container view controller that contains it. So this would be closer:

UINavigationController *nav = self.navigationController;
[nav popToRootViewControllerAnimated:YES];

UIViewController *rootVC = [nav.viewControllers objectAtIndex:0];
[rootVC performSegueWithIdentifier:@"GoToB" sender:self];

But, I think the problem here will be that the pop animation will conflict with the segue. Doing the pop with ...Animated:NO might fix it, but I think it would be more correct (and more robust for animations) to perform the segue from the rootVC.

rootVC would implement viewDidAppear as follows:

- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];
    if (!self.isBeingPresented && /* any other condition that makes you want this */) {
        [self performSegueWithIdentifier:@"GoToB" sender:self];
    }
}


来源:https://stackoverflow.com/questions/14417370/pop-to-root-then-perform-segue-on-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!