Views and View Controllers
In iOS development, the view controllers are the foundation of the Application’s internal structure. The View Controller is the parent of all the views present on a storyboard. Each application has at least one ViewController. It facilitates the transition between various parts of the user interface.
The UIViewController is the parent class of all the ViewControllers. It defines all the methods and properties for managing our views. This class also manages the events and transitions from one view controller to another. It also coordinates between the different parts of the application.
Types of ViewControllers
There are two types of ViewControllers:
- Content ViewController: Content ViewControllers are the main type of View Controllers that we create. The Content View Controllers holds the content of the Application screen. In other words, we can say that the Content View Controller manages the discrete piece of the application content. The Content ViewController manages all the Views itself.
- Container ViewController: Container ViewController is different from content ViewController in the sense that it acts as a parent View Controller, which collects information from the child view controllers. The task of the container view controller is to present the collected information to facilitate the navigation to the child view controllers. The container ViewController only manages the RootView, which incorporates one or more Child ViewControllers.
Most iOS applications are the mixture of both, Content ViewController and Container ViewController.
View Management
In iOS development, the ViewController manages the hierarchy of views. As shown in the below image, each ViewController contains a RootView which contains all the content of the view controller. All the custom views needed to maintain an iOS application is added to the root view to display the content. The following figure shows the relationship between ViewController, RootView, and its subviews. Each sub view is referred by a Super View which incorporates a chain of Views where a RootView acts as the Parent View of all the views present in the View Controllers.
Data Marshaling
In iOS Development, a View Controller is responsible for displaying the data of our iOS application on the screen. It acts as an interface between its Views (created by the developer) and the application data. Each ViewController in the Storyboard is assigned a Class that inherits the UIViewController class.
All of the properties and methods defined in the UIViewController is present in the class that we assign to the ViewController. However, for the development of our application, we need to define our properties and methods in the ViewController class. It helps us manage the visual representation of our application.