My app has a main view with a few buttons lined up vertically. Each button is linked to a different view controller. Now, when the user taps on the first button, the first view controller is opened. Within the first view controller, there is a button which takes you directly to the second view controller. When I get to the second view controller, and press the back button, I am taken back to the first view controller. Then I have to press the back button one more time to be taken to the main view controller. What can I do to make the app take me directly to the main view controller from the second view controller, and not through the first view controller? Thanks in advance!
Asked
Active
Viewed 2,428 times
0
-
possible duplicate of [How to go back to RootViewController from presentView controller?](http://stackoverflow.com/questions/26755916/how-to-go-back-to-rootviewcontroller-from-presentview-controller) – Chris Slowik Sep 14 '15 at 18:53
4 Answers
0
You could bypass the segue_unwind to go back to the main view controller. Just call the performSegueWithIdentifier in the IBAction of the button
Swift
self.performSegueWithIdentifier("SegueIdentifierName", sender: self)
Objective C
[self performSegueWithIdentifier:@"SegueIdentifierName" sender:self];
Here is a similar article as well What are Unwind segues for and how do you use them?

Community
- 1
- 1

Nick Hayward
- 178
- 2
- 12
-
I understood everything the answers on the question in the link said except one thing. One step was to link the button to the exit. But, I don't have a button. It is the back button on the navigation bar itself. What can I do then? – Krish Wadhwana Sep 14 '15 at 19:23
0
There are a lot of answers in SO about this BUT:
[self.navigationController popToRootViewControllerAnimated:YES];
OR
UIViewController *rootViewController = [self.navigationController.viewControllers firstObject];
[self.navigationController pushViewController:rootViewController animated:YES];
Should work just fine.
Next time, TRY to find answers first: How do I get the RootViewController from a pushed controller?
-
1
-
1@KrishWadhwana you cannot do this. Instead, remove the 1st view controller from the stack, when you push the 2nd. – NSPunk Sep 17 '15 at 14:05
-
1`NSMutableArray *vc = [[self.navigationController viewControllers] mutableCopy]; [vc removeObjectAtIndex:1]; [self.navigationController setViewControllers:vc];` – NSPunk Sep 17 '15 at 14:12
0
You can override the back button action to call popToViewController() on the navigationController with the view controller you would like to return to.

robinkunde
- 1,005
- 9
- 12
0
SWIFT 3.01
self.performSegue(withIdentifier: "SegueIdentifierName", sender: self)

Yaroslav Dukal
- 3,894
- 29
- 36