Kalzem
Kalzem

Reputation: 7492

Pop to root then perform segue on iPhone

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 ?

Upvotes: 1

Views: 2474

Answers (1)

danh
danh

Reputation: 62676

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];
    }
}

Upvotes: 2

Related Questions