Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
A view is an object that can be drawn on the screen that the user can see. Other views (subviews) can be included within it and can be moved along with it. Touch events can interact with views and inform listeners or delegates about any events that occur in them. Views display themselves without actually knowing the structure of the application. Examples include a text label, a button, a checkbox, etc.
A view controller, unlike views, is not drawable to the screen directly. They are the foundation of an app's structure. Every app consists of at least one view controller. It manages a group of view objects. View controllers consist of a single view of subviews and manage their state. Unlike the views, a view controller is intelligent and can interact with the application. For example, a screen that shows a list of users' edits, user forms, and a login screen.
A view controller acts as a glue between your overall application and the screen. It controls the views according to the logic of your application.
Types of View Controllers
There are two types of view controllers: -
Content ViewController
These view controllers are the primary controllers that hold the content of the application's screen. Thus it maintains a discrete piece of application content. Therefore, the Content ViewController manages all the Views itself.
Container ViewController
It acts as a parent view controller that collects information from the child view controllers. It helps in presenting the collected data to facilitate the navigation to the child view controllers. It only manages the RootView, which includes one or more Child ViewControllers.
View controllers can manage content from other view controllers
View Management
The ViewController maintains the view hierarchy in iOS programming. Each ViewController, as seen in the figure below, has a RootView that holds all of the view controller's content. To show the content, all custom views required to maintain an iOS application are added to the root view.
The diagram below depicts the connection between ViewController, RootView, and its subviews. A Super View refers to each subview, which includes a chain of Views, with a RootView acting as the Parent View of all the views in the View Controllers.
Relationship between a view controller and its views
Data Marshalling
A View Controller is responsible for showing the data of our iOS application on the screen in iOS development. It serves as a link between the developer-created Views and the application data. A Class is allocated to each ViewController in the Storyboard, inheriting the UIViewController class.
The class we assign to the ViewController contains all of the attributes and methods declared in the UIViewController. However, we must declare our properties and methods in the ViewController class to construct our application. It assists us in managing our application's visual representation.
A view controller mediates between data objects and views
User Interactions
Responder objects, such as view controllers, can handle events sent down the responder chain. View controllers seldom run touch events directly, even though they can respond.
Instead, views typically handle their touch events and submit the results to a method of a delegate or target object that is generally the view controller. Delegate methods or action methods are used to handle most events in a view controller.
Resource Management
A view controller is entirely responsible for the views and objects it generates. Most parts of view management are handled automatically by the UIViewController class. UIKit, for example, releases any view-related resources that are no longer required. You are responsible for handling any objects you create directly in your UIViewController subclasses.
When free memory becomes scarce, UIKit asks programs to give up any resources they no longer require. One way it achieves this is by invoking your view controllers' didReceiveMemoryWarning function. This way can be used to get rid of references to items you don't require or can restore afterward.
Adaptivity
View controllers are in charge of how their views are presented and how they adjust that presentation to the underlying environment. Every iOS software should be able to run on the iPad and a variety of iPhone sizes. Therefore, it's easier to employ a single view controller that adapts its views to changing space needs than provide various view controllers and view hierarchies for each device.
View controllers in iOS must be able to manage both coarse and fine-grained changes. When the attributes of a view controller change, coarse-grained modifications occur. The display scale, for example, is an example of a trait that describes the entire environment.
Adapting views to size class changes
The horizontal and vertical classes, which specify how much space the view controller has in the given dimension, are two of the essential properties. As demonstrated in the Figure below, you can modify how your views are laid up by changing the size class. When the horizontal size class is regular, the view controller arranges its content to make use of the increased flat space. The view controller organizes its content vertically when the horizontal size class is compact.
Frequently Asked Questions
What are responder objects?
A responder is an object that responds to events, such as touching events and handling them. They are basically instances of classes that ultimately inherit from UIResponder (iOS) or NSResponder. These classes define default behavior for responders and declare a programmatic interface for event handling. Examples include visible objects like windows, views, and controls.
What is the difference between view and view controllers?
A view is an object that can be drawn on the screen that the user can see. Other views (subviews) can be included within it and can be moved along with it. Touch events can interact with views and inform listeners or delegates about any events that occur in them.
A view controller, unlike views, is not drawable to the screen directly. They are the foundation of an app’s structure. Unlike the views, a view controller is intelligent and can interact with the application.
What is a modal view controller?
Model–view–controller is a software architectural pattern that divides related program logic into three companion pieces and is often used for building user interfaces. This distinguishes internal information representations from how information is presented to and accepted by users.
Conclusion
This article extensively discussed View and View Controllers and their uses.