Using UINavigationController

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:

  1. Double-click on the MainStoryboard.storyboard file to open it in Xcode.
  2. Remove the controller that was created by the project template.
  3. On the bottom-right of the dialog, drag a Navigation Controller element onto the storyboard.
  4. Remove the Table View Controller element that was created automatically.
  5. Drag a new View Controller element next to Navigation Controller.
  6. Right-click and drag the Navigation Controller element to the new View Controller element. You will first see a blue line appear and then a pop up when you release.
  7. Under Relationship Segue, select root view controller.
  8. You will see a segue that connects the two controllers. We'll cover this concept in more detail later in the chapter.
  9. Save the storyboard file and return to Xamarin Studio.

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:

Using UINavigationController
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.31.73