Avoiding Massive View Controller using Containment & Child View Controller

It also setup the layout and adding the child view controllers using the container views.You can view the complete project source code in the GitHub Repository below.alfianlosari/Filter-MVC-iOSUsing Child View Controllers for encapsulation, reusability, and avoid Massive View Controller …github.comComposition of the View Controllers using StoryboardView Controller Compositions using StoryboardLooking at the storyboard above, here are the View Controllers that we use to build our Filter screen:ContainerViewController: The Containment View Controller that provides 2 container views to embed the Child View Controller inside a horizontal UIStackView..It also provides single UIButton to clear the selected filters..It also embedded in a UINavigationController that acts as initial View Controller.FilterListMovieController: The View Controller that is the subclass of the UITableViewController with Grouped style and one prototype standard Cell to display the name of the filter..It also has its Storyboard ID assigned so it can be instantiated from the ContainerViewController programatically.MovieListViewController: The View Controller that is the subclass of the UITableViewController with Plain style and one prototype subtitle Cell to display the attributes of the Movie..It also has its Storyboard ID assigned like the FilterListViewController.The Movie List View ControllerThis View Controller has the responsibility to dis play the list of Movie model that is exposed as an instance property..We are using Swift didSet property observer to react to changes in model then reload the UITableView..The Cell displays the title, duration, rating, and genre for the Movie using default subtitle UITableViewCellStyle.The Filter List View ControllerThe Filter List displays the MovieFilter enum in 3 separate sections, they are genre, rating, and duration..The MovieFilter enum itself conforms to Hashable protocol so it can be stored inside a Set uniquely using the hash value of the each enum and its properties..The selection of the filters is stored under an instance property with Set containing the MovieFilter.To communicate with other object, a delegate pattern is used using the FilterListControllerDelegate..There are 3 methods for the delegate to implement:Selection of a filter.Deselection of a filter.Clear all selected filters.Integrating inside the Container View ControllerIn the ContainerViewController, we have several instance properties:FilterListContainerView & MovieListContainerView: The container views that will be used to add the child view controllers.FilterListViewController & MovieListViewController: The reference to Movie List and Filter List View Controllers that will be instantiated using the Storyboard ID and assigned to.Movies array that is instantiated using default hardcoded Movies.When the viewDidLoad is invoked, we call the method to setup the Child View Controllers..Here are several of the tasks it performs:Instantiate the FilterListViewController and MovieListViewController using the Storyboard IDAssign them to the instance properties.Assign the MovieListViewController the movies array.Assign the ContainerViewController as the delegate of FilterListViewController so it can responds to the filter selection.Set Child Views frames and add them as the Child View Controller using the helper method extension.For the FilterListViewControllerDelegate implementation, when filter is selected or deselected, the default Movies data is filtered for each respective genre, rating, and duration..Then, the result of the filter is assigned to the MovieListViewController movies property.. More details

Leave a Reply