iOS: UI View Controller
The fundamental components of iOS applications are ViewControllers, as we have already mentioned in this course. In order to develop an iOS application, the content views are drawn on a container in the storyboard called a ViewController. On the other hand, an iOS application’s ViewControllers are in charge of handling its content views.
A range of ViewControllers, such as UIViewController, TableViewController, CollectionViewController, PageViewController, etc., are used in iOS development to handle the content views. In this tutorial portion, we will talk about UIViewController.
An object called a UIViewController is in charge of overseeing the UIKit application’s view hierarchy. All ViewController types used in the iOS application have their common characteristics and behavior defined by the UIViewController. The UIResponder class is inherited by the UIViewController class.
class UIViewController : UIResponder
View Management
The class’s view property contains the root view in the view hierarchy. The view hierarchy’s root view serves as a container for all other views. The object that owns the root view—either the app’s window or a parent view controller—determines its size and location. The app’s root view controller is the one that the window owns, and its view is sized to fill the window.
We need to provide the views in order to construct the content views in the iOS application. The views in iOS applications can be specified using the following methods.
1) Using a storyboard is the best method for defining custom views. The storyboard allows us to designate the view and establish the link between the view and the appropriate ViewController class. The storyboard itself allows us to define the connections between the application’s various ViewControllers. We use the UIStoryboard class’s instantiateViewController(withIdentifier:) function to load a view controller from the storyboard. The viewcontroller object is created by the storyboard object and then returned to the code.
2) A Nib file can be used to specify the views. Though it does not allow us to build relationships between distinct ViewControllers, the nib file does make it easier to declare the views for a single viewcontroller class.
3) The loadView() method allows us to define the views for a ViewController by programmatically creating the view hierarchy and assigning the root view to the UIViewController view property.
The ViewController’s root view in the storyboard is always sized to fit in the ViewController’s designated space. The auto-layout constraints that control the size and placement of the views on various screen sizes must be defined for each custom view we add to the storyboard.
View States
The subclass controls the listener methods, which are called when the view appears or vanishes, in response to changes in the viewcontroller’s appearance. The methods and the corresponding view states are displayed in the following image.
The following methods are alerted when the viewcontroller’s appearance changes.
viewWillAppear(): This function is called just before the viewcontroller appears. Using this strategy, we get all of our views ready to appear on screen.
viewDidAppear(): Upon the appearance of the view controller, this function is called. The code for this function will run as soon as the
viewWillDisappear(): When the view controller is ready to disappear, this method gets informed. Using this approach, we may insert the code to save the modifications or other state data.
ViewDidDisappear(): when the view controller vanishes, this method is alerted.
Implementing Container ViewController
The Container ViewController controls how the content of other view controllers it owns—also referred to as the child view controllers—is presented, serving as a container for the other view controllers. The container view controller may also be a custom ViewController.
You can use the following way to keep the view controllers for containers up to date.
- addChild(: )
- removeFromParent(: )
- willMove(toParent: )
- didMove(toParent: )
Memory Management
The iOS application can release unused memory by using the built-in support of ViewControlles. This effectively stops any memory leaks in the program. A lifecycle method called didReceiveMemoryWarning() is available in the UIViewController class, and it receives notification of low-level memory conditions.