You can achieve this by intercepting the back button event in ViewController3 and using unwindSegues.
Check out William Jockusch's reply to this question for how to intercept the back button event.
To use the unwind segues in this particular case you then need to:
1) In your ViewController1 create a method such as
- (IBAction)unwindToThisViewController:(UIStoryboardSegue *)unwindSegue
{
NSLog(@"Rolled back");
}
2) In your storyboard zoom out, then ctrl-drag from your ViewController3 to the "Exit" green box on the left contained in your ViewController3 scene (along with the red box First Responder and all your controller view's subviews). A popup window will show, asking what IBAction you want to connect the unwind segue to, and you should be able to select the unwindToThisViewController
action that you just created. This will create an unwind segue.
3) Select from the scene box this unwind segue and give it an ID, such as "unwindToStart"
4) Finally, in your ViewController3 class, override the method viewWillDisappear
as follows:
-(void) viewWillDisappear:(BOOL)animated
{
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound)
[self performSegueWithIdentifier:@"unwindToStart" sender:self];
[super viewWillDisappear:animated];
}
This will intercept the back button event and unroll to your ViewController1, and you're good to go.
EDIT: As unwind segues are supported only on iOS 6 and later, if you need this functionality on earlier versions of iOS I think the only way is to manually remove the ViewController2 from the NavigationController stack in your ViewController3's viewDidLoad. Something like the following code should do:
- (void)viewDidLoad
{
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
for(UIViewController* vc in self.navigationController.viewControllers)
{
if ([vc isKindOfClass:[ViewController2 class]]) {
[viewControllers removeObject:vc];
break;
}
}
self.navigationController.viewControllers = [NSArray arrayWithArray:viewControllers];
// Do any additional setup after loading the view.
}