Xamarin Development for the Mac - Navigating Between View Controllers
By Dawid Borycki
Although tabbed and page-based project templates can be utilized to quickly create multi-view iOS applications, these project templates are not always suitable. In such cases, you can manually create segues between view controllers. When the user presses a particular control, the selected view controller will be presented. In this article, I’ll show you how to manually create such a custom multi-view app of name Navigation. As Figure 1 shows, the app will have two view controllers: First View Controller and Second View Controller. Both of them will have associated views with labels describing a view controller. The user will be able to switch between views using Switch view or Go back buttons, depending on the currently displayed view.
Figure 1. Views of the Navigation app, which we will develop in this section
To implement the Navigation app, I use the Single View App project template, which targets iOS 9.0 and above. Then, I modify the view by adding a button and a label, configuring them as shown in the left part of Figure 1. Subsequently, I set the class of the initial view controller to FirstViewController, then add another view controller and set its class to SecondViewController. Then, I modify the view of the second view controller as shown in the right part of Figure 1. Lastly, I remove the ViewController.cs file, because it is not needed anymore.
Given the view controllers are ready, I define the first segue. As shown in Figure 2, I CTRL+Drag from the Switch view button to the second view controller. After releasing the left mouse button, a popup window appears. This popup allows me to choose the segue, which presents a new view controller. There are eight segue types to choose from. However, three of them (Non-Adaptive Action Segue group) are deprecated, and thus I do not discuss them. The other segues are the following:
- Show – displays the view of the selected view controller modally over the content of the current or source view controller. This segue can be also done programmatically with the ShowViewController method of the UIViewController.
- Show Detail – this segue corresponds to the UIViewController.
- ShowDetailViewController method and is only valid for the UISplitViewController. In this case, Show Detail replaces the second child view controller with the selected view controller. For other view controllers, Show Detail works like the Show segue.
- Present Modally – presents a view controller modally. Basically, it works similarly to the Show segue, but you can modify its presentation and transition styles.
- Popover Presentation – the effect of this segue depends on the actual size class. For screens with regular widths, the target view controller will appear as a popover, and modally (full-screen) on horizontally compact devices.
- Custom – custom-created segue. You can use this if none of the others are suitable.
Figure 2. Creating a segue between view controllers.
Here, I choose the Show segue, so after re-running the Navigation app, a view of the Second View Controller appears when you tap the Switch view button.
About the Author:
Dawid Borycki is a software engineer, biomedical researcher, and an expert in several Microsoft developer technologies. He has resolved a broad range of software development challenges for device prototypes (mainly medical equipment), embedded device interfacing, and desktop and mobile programming. Dawid regularly speaks at international developers conferences and has published, cited, and presented on numerous developer topics.This article is excerpted from his book Beginning Xamarin Development for the Mac (December 2017).
Want more? Pick up Dawid's book, Beginning Xamarin Development for the Mac, now available on Apress.com.