Programmatically build / navigate a Navigation Controller
In applicationDidFinishLoading
in App delegate:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[window makeKeyAndVisible];
[window addSubview:navController.view];
That will instantiate the navigation controller and add it to the window as a view.
Now, in your rootViewController class (lets say its called FirstViewController
) you can do this:
- (void)clickedAButton:(id)selector {
SecondViewController *nextViewController = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
// and push it onto the 'navigation stack'
[self.navigationController pushNavigationController:nextViewController animated:YES];
// and release
[nextViewController release];
}
And in your SecondViewController
you can navigate back through the stack using:
- (void)clickedAnotherButton:(id)selector {
// goes back to the last view controller in the stack
[self.navigationController popViewControllerAnimated:YES];
}
So for you it would go:
Set up navigation controller in the app delegate with Brand
as the root view controller
User chooses their brand and you pushViewController:animated:
the Model
view controller. Then the user chooses their model and you pushViewController:animated:
the Color
view controller. Similarly the user chooses a color and you push the Pippo
view controller. Now, if the user presses back (or you call popViewControllerAnimated:
) it will go back to the Color
view controller in the same state as when the user left it to go to the Pippo
controller.
Comments
-
Fabio B. almost 2 years
Before:
My App is based on indepent view controllers. I can switch from one to another by replacing the root view controller on the application delegate:
ade.window.rootViewController = newController;
... and all worked right, till now.
Tomorrow:
we have to add a NavigationController-based part of our App, which will help the users navigate through our:
Brands => Model Names => Colors
So, the user will choose a color, then click a button: now I will switch to another UIViewController (call it "pippo"), which actually resides outside that navigation hierarchy (I can't push it in the nav-controller for several methods, I'm forced doing so!).
What I want is to get back to my "Color" screen, from "pippo". So, I'm looking for a way to programmatically "navigate" the navigation controller I restore, I mean:
I restore my navigation controller
now I'm on Brands, but I don't want my users to be here, I want to show them the last color they was on (I saved it in the preferences)
how can I simulate the selection of a known brand and model?
Thanks a lot.
-
Fabio B. over 12 yearssoorry, wrong answer for two reasons: i quote from my post: ""pippo"), which actually resides outside that navigation hierarchy (I can't push it in the nav-controller for several methods, I'm forced doing so!)." So you know that I CAN'T and I DON'T WANT to push 'pippo' in my navigation controller. - in addition, I want to navigate the controller WITHOUT user interaction. :-\
-
Thomas Clayson over 12 yearsI can't think of a single scenario where you can't push a viewcontroller onto a navigation stack. However, you're the boss. Navigating the stack without user interaction is possible using the methods push and pop. Anyway, can't help you, as I don't really understand anymore.
-
Fabio B. over 12 yearsMy 'pippo' is a custom imagepicker, a photo camera. However I'll try to push my brand and model view controller without animation and see what it does.
-
Thomas Clayson over 12 yearsOk cool. You can add your own animations by using UIView animation around where you
pushViewController:animated: