0

I am using only segue to navigate in my app.

I just like to know if I navigate from 1st-VC to V2 to V3 then to 4th-VC, how can I navigate back to 1st VC from 4th-VC directly without going back to V3, V2 then VC1?

Thanks.

In my current app, I use this to navigate back to the previous VC (using unwind):

@IBAction func unwindToViewController (sender: UIStoryboardSegue){

    }

Is this good practice?

Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59
MilkBottle
  • 4,242
  • 13
  • 64
  • 146
  • I highly recommend you using a navigationcontroller in your case as it will bring you the things you require. Yo dont need to unwind a segue with the pop...Viewcontroller functions comming with navigationcontroller. – Arbitur Jun 10 '15 at 08:19
  • This will help you - http://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-do-you-use-them – iDhaval Jun 10 '15 at 09:01

3 Answers3

0

you may use below code -

func popToRoot() {
        self.navigationController?.popToRootViewControllerAnimated(true)
}
Santu C
  • 2,644
  • 2
  • 12
  • 20
  • Not working. I dont use navigationController. There is no compilation error but it wont go to the 1st VC as I am not using Navigation Controller. – MilkBottle Jun 10 '15 at 08:14
0

You can create IBAction in 4th-VC and call popToRootViewControllerAnimated method.

Greg
  • 25,317
  • 6
  • 53
  • 62
  • I used the IBAction like self.navigationContoller?.popToViewControllerAnimated(true) as there is no self.popToViewControllerAnimated(). It is not working. I think I am not using Navigation Controller in my app. I just use Segue and the above mentioned method to go Back. Is there any other way? – MilkBottle Jun 10 '15 at 08:18
0

I know of three options:

The first (and probably the best) is to use an unwind segue. They are greatly explained in details here (SO question)

The second one, if your VC1 is the root view controller, is to simply use this line in your VC4:

self.navigationController?.popToRootViewControllerAnimated(true);

The last one is to manually set the ViewControllers stack to the state that you want (note, that you're going to get references to your VCs somehow). For example, "go three VCs back" (sorry, autocomplete suddenly broke down in Xcode for Swift, so Objective-C only):

UINavigationController *navigationController = self.navigationController;
NSMutableArray *newVCs = [navigationController.viewControllers mutableCopy];

//This part would be more elegant in Swift, of course
[newVCs removeLastObject];
[newVCs removeLastObject];
[newVCs removeLastObject];

[navigationController setViewControllers:newVCs animated:YES];

The first solution is the best, because it is the most flexible. And it doesn't make you hardcode your VCs stack. But it really depends on your requirements. The second one is by far the easiest to implement, so if you need to go back to the root VC, it is the way to go. As for the third one, it's somewhat easier to implement than the first one, but I think that the first one is the most correct.

Community
  • 1
  • 1
FreeNickname
  • 7,398
  • 2
  • 30
  • 60