View Controllers and Navigation


Navigation Controllers

Navigation controllers serve as “container” view controllers that work like a stack data structure. The navigation bar automatically appears via the Navigation Controller and allows users to easily move through the different view controllers of our application. The Root View Controller serves as the initial screen for the Navigation Controller. We create Navigation Controllers on the storyboard.

To embed your view controller in a Navigation Controller, click on the view controller and at the top click Embed In -> Navigation Controller.

This will show a navigation bar on the top of every view controller.

If you want to hide the navigation bar, you could call this method in the viewDidLoad method of your view controller.

self.navigationController?.setNavigationBarHidden(true, animated: false)

View Controller LifeCycle

Here are the possible states of a viewController in order:

  • loadView: Called when the view controller is trying to create its view
  • viewDidLoad: Called when view controller initially loads onto the screen
  • viewWillAppear: Called when the view controller is about to be added to the view hierarchy
  • viewDidAppear: Called when the view controller has been added to the view hierarchy
  • viewWillDisappear: Called when the view is about to disappear from the view hierarchy
  • viewDidDisappear: Called when the view has disappeared from the view hierarchy

Model-View-Controller (MVC)

The model view controller design pattern is the best way to compartmentalize your code into specific sections, namely: the model, the view, and the controller.

Model: Where your data resides and is responsible for all the logic of your application

Views: All the UI elements of your application and what gets shown to the User

Controller: Whatever calls your views and models (in our case a view controller)

Here’s the step by step process that shows how the Model, View, and Controller interact:

  1. View passes User action to the Controller
  2. Controller updates the Model
  3. Model notifies the Controller
  4. Controller updates the View

Other tips to cleanup your project code:

  • Use a Model class to seperate out the logic from your viewController files
  • Put UI element creations outside of the viewDidLoad method in seperate functions
  • use Extensions to seperate code into different parts
  • Group similar files into folders

Github

Collaborating in Github and Xcode:

  • setup a .gitignore to make sure Git ignores certain files
  • run git pull origin master before every commit! (unless youre using branches)
  • run git status if you’re having trouble with merge conflicts

Table and Collection Views

Table Views

  • Simply a list of items
  • Made up of UITableViewCells
  • Subclass of UIScrollView and supports vertical scrolling
  • Requires the UITableViewDelegate and UITableViewDataSource

Required Methods: cellForRowAt and numberOfRowsInSection and didSelectRowAt

Creating a UITableViewCell:

  • Create a class that is a subclass of UITableViewCell
  • Implement an awakeFromNib() function (think of the awakeFromNib() function as a viewDidLoad() method for UITableViewCells)
  • Inside your UITableViewDelegate use the .dequeReusableCellwithIdentifier method within your cellForRowAt function

Collection Views

  • Simply a collection of items
  • Made up of UICollectionViewCells
  • Subclass of UIScrollView and supports vertical and horizontal scrolling
  • Requires the UICollectionViewDelegate and UICollectionViewDataSource

Required Methods: cellForItemAt and numberOfSections and numberOfItemsInSection

Creating a UITableViewCell:

  • Create a class that is a subclass of UICollectionViewCell
  • Implement an awakeFromNib() function (think of the awakeFromNib() function as a viewDidLoad() method for UITableViewCells)
  • Inside your UICollectionViewDelegate use the .dequeReusableCellwithIdentifier method within your cellForItemAt function

Protocols and Delegates

Protocols (Think Contract): A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements.

Delegate (Think Communication): Delegation is a design pattern that enables a class or structure to hand off (or delegate) some of its responsibilities to an instance of another type.