View Controller is the part that gives the essential constructing block we use as a foundation for creating an utility in iOS improvement. Within the Apple MVC world, it stands as an middleman between the view and the mannequin, taking part in the position of orchestrator between the 2. It begins with the controller performing as an observer who reacts to mannequin modifications, updates the view, accepts the person's interactions from the view with the assistance of the person. goal motion, after which replace the mannequin.
As an iOS developer, we’ll usually expertise an enormous Huge View Controller administration drawback, even when we use architectures corresponding to MVVM, MVP or VIPER. Generally the View Controller has too many obligations to handle in a single display. This violates the precept of sole duty (SRP), creates a good coupling between modules and makes it tough to reuse and take a look at every of the parts.
We are able to take the display seize of the appliance beneath for instance. You possibly can see that there are no less than three obligations on one display:
View an inventory of flicks
Show a filter listing that may be chosen to be utilized to the film listing;
Clear the number of the chosen filters.
If we construct this display with the assistance of a single view controller, it’s assured that the view controller will grow to be very large and inflated as it can assume an excessive amount of duty in a single view controller.
How can we clear up this drawback? One of many options is to make use of View Controller Containment and Youngster View Controller. Listed here are some great benefits of utilizing this resolution:
Encapsulate the film listing in MovieListViewController which is solely liable for simply displaying the film listing and reacts to the modifications made in Film Mannequin. We are able to additionally reuse this MovieListViewController in one other display if we solely need to show the listing of flicks with out filter.
Encapsulate the listing and number of filter logic in FilterListViewController which is solely liable for displaying and processing the filter choice. We are able to use delegation to speak with the mum or dad View Controller when the person selects and deselects filters.
Scale back the primary view controller to a ContainerViewController container that solely applies the filters chosen within the filter listing to the film template in MovieListViewController. It additionally configures the structure and provides the kid view controllers with the assistance of container views.
You possibly can see the entire supply code of the challenge right here within the GitHub repository beneath.
Composition of the view controllers with the assistance of Storyboard
In response to the storyboard above, listed here are the view controllers we use to construct our filter:
ContainerViewController: The containment view controller offers two container views for embedding the kid view controller right into a horizontal UIStackView. It offers a single UIB button to additionally clear the chosen filters. Additionally it is built-in with a UINavigation controller that acts because the preliminary view controller.
FilterListMovieController: The view controller that’s the subclass of UITableViewController with the Grouped fashion and a regular prototype cell to show the title of the filter. Its Storyboard ID can also be assigned to it in order that it may be instantiated from ContainerViewController programmatically.
MovieListViewController: The view controller that’s the subclass of UITableViewController with the Plain fashion and a subtitle prototype cell to show the attributes of the film. Its Storyboard ID can also be assigned to it, like FilterListViewController.
The show controller of the listing of flicks
This view controller is liable for displaying the listing of uncovered film templates for instance property. We use Swift, the observer of the didSet property, to react to mannequin modifications after which reload UITableView. The cell shows the title, length, score, and style of the film utilizing the default UITableViewCellStyle subtitle.
let title: String
let style: String
depart length: TimeInterval
depart observe: Float
MovieListViewController class: UITableViewController
] Movie [19459007 ] Title : ] style : Chain
  Period  007] : ] Let analysis : Flotter
 Class MovieListViewController : UITableViewController 
VAR  [ Movie ] ]
Didet Not but ]
TableView . [1945
]. ] :: DateComponentsFormatter . let coach