In iOS applications, the key class for managing navigation between different controllers is the UINavigationController
class. It is a parent controller that contains several child controllers in a stack. Users can move forward by putting new controllers on top of the stack or using a built-in back button to pop a controller off the stack and navigate to the previous screen.
The developer can manipulate the navigation controller's stack with the following methods:
SetViewControllers
: This sets an array of child controllers. It has a value to optionally animate the transition.ViewControllers
: This is a property for getting or setting the array of child controllers without an option for animations.PushViewController
: This places a new child controller at the top of the stack with an option to display an animation.PopViewControllerAnimated
: This pops off the child controller at the top of the stack with an option to animate the transition.PopToViewController
: This pops children off the stack until reaching the specified child controller, removing all controllers above it. It provides an option to animate the transition.PopToRootViewController
: This removes all child controllers except the bottom-most controller. It includes an option to display an animation.TopViewController
: This is a property that returns the child controller that is currently on top of the stack.It is important to note that using the option for animations will cause a crash if you try to modify the stack during the animation. To fix this situation, either use the SetViewControllers
method and set the entire list of child controllers, or refrain from using the animations during a combination of transitions.
Let's set up a navigation controller in our application by performing the following steps:
MainStoryboard.storyboard
file to open it in Xcode.If you run the application at this point, you will have a basic iOS app with a status bar at the top, a navigation controller containing a navigation bar, and a child controller that is completely white, as shown in the following screenshot:
3.19.31.73