I’m trying to implement a navigation controller with some hierarchical views. I want to use a regular UIViewController to present choices for drilling down, I don’t want to use the navigation bar – I want to have my own, custom buttons for returning back up a level.
I see examples like:
[[self navigationController] pushViewController:nextViewController animated:YES];
and my questions are these: Is navigationController a property of all UIViewControllers? Can I refer to self.navigationController regardless of the view that’s on the stack? If I’m at an arbitrary view, can I have a button action that contains something like [self.navigationController popToRootViewController animated:YES];
Each view I present will need a button to return to the previous view, or to the root view, depending on the situation. I want to create that button in each view controller and control which view in the stack it returns to. Am I on the right track?
Yes.
Every
UIViewControlleron theUINavigationController‘s stack will return theUINavigationControllerobject when callingnavigationControlleron it.Yes.
popToRootViewControllerAnimated:will take the user to the rootUIViewControllerfor theUINavigationController, and you can use[self.navigationController popViewControllerAnimated:YES];to just pop off the topUIViewController. This last one does the same as tapping theBackUIBarButtonItem.Yes 🙂