0

I have 3 view controllers : A ->segue1-> B ->segue2-> C

When the user is on "C" and press the "back" button, I would like to go directly to A instead of B. How can I do that with swift ?

Thanks

Fab
  • 121
  • 1
  • 7

1 Answers1

0

If you are using navigation controller you can use popToRootViewControllerAnimated(_:).

In case you have presented both controllers modally you can call dismissViewControllerAnimated(_:completion:) in your A controller.

Create your own back button :

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonSystemItemDone target:self action:@selector(tapped:)];

And pop to root controller :

-(void)tapped:(UIButton*)sender {
   [self.navigationController popToRootViewControllerAnimated(NO)];
}
stefos
  • 1,235
  • 1
  • 10
  • 18
  • override func willMoveToParentViewController(parent: UIViewController?){ self.dismissViewControllerAnimated(true, completion: {}); } displays B from C but not A. – Fab May 24 '16 at 13:30
  • You dont't need willMoveToParentViewController. This is used for adding child controller inside another controller. Just call popToRootViewController if you are using push segue(navigation contorller) or dismissViewController in your A controller object. – stefos May 24 '16 at 13:34
  • I need to override the back button in the C screen. Instead of C->B, I want C->A – Fab May 24 '16 at 13:43
  • Use popToRootViewControllerAnimated in your back button action. – stefos May 24 '16 at 13:46
  • I don't have a back button in my storyboard. The back button is created automatically by the navigation controller. popToRootViewControllerAnimated displays a black screen :( – Fab May 24 '16 at 13:57