Ios – Jumping between views in Navigation controller

iosuinavigationcontrollerxcode

I need to jump between view controllers. For example:

View1: First screen (Just logo)
View2: Download Screen
View3: First app screen (Some Buttons)
View4-View(N): some app screens

When user enters app the app goes to View1->View2 (downloads stuff)->View 3->View4->View5
Then user wish to go to First app screen (View3) he does:

NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:2] animated:NO];

The first time user enters the app it goes: View1->View3 (The download screen no longer needed), and I have different push segue to go to View3 so lets assume the user goes to: View1->View 3->View4->View5, now he wishes to go back to View3, so the function:

NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:2] animated:NO];

Will return him to View4, and this is WRONG. How can I solve it?

Best Answer

Well if you are using storyboards , what you can do is set your uiviewcontrollers' storyboard id and use it for popping and pushing your views.

enter image description here

Lets say your Storyboards name is MainStoryboard.storyboard

UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                  bundle:nil];
SettingsListViewController *settingsVC = [sb instantiateViewControllerWithIdentifier:@"SettingsListViewController"]; // @"SettingsListViewController" is the string you have set in above picture
[self.navigationController popToViewController:settingsVC animated:YES];

Above solution should work for you , but If I were you I would change the structure of my app , you say :

View1: First screen (Just logo)
View2: Download Screen

Since View1 is just logo and View 2 is also a view that you skip, you can remove them from navigation controller and make View3 your navigation controller's root view controller and when you need view1 and view2 present them as Modal View Controllers,

when you are done with them lets say; user successfully loaded the app dismiss logo screen and present Download Screen if download successful then dismiss it.

So your View3 Will be there as root view controller, lets say your at View(n) you want to go to home screen which is View3 all you need to do is call

[self.navigationController popToRootViewControllerAnimated:NO];

When you are on view(n) and want to pop to view(n-1) just use

[self.navigationController popViewControllerAnimated:YES];

good luck,

Related Topic