Notes Taken by Shubham Gupta

Recap From Lesson 1

Building off of our CoinFlip app from last time, we notice that after we predict the coinflip and see our result, we have no way of returning back to the home screen. This is terrible User Experience and we definitely want to do something about this. Today we will be focusing on creating more usable apps as well as learning some core swift concepts.

Navigation controllers serve as “contrainer” view controllers that works like of 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.

Some Important Navigation Controller Related Functions/Properties to Know:

  • .isHidden (set to True if you want to hide the navigation bar at the current ViewController)

View Controller LifeCycle

Here are the possible states of a viewController in order:

  • loadView: Automaticaly called when viewController is accessed
  • viewDidLoad: Called when viewController initally loads onto the screen
  • viewWillAppear: Called when the viewcController is about to be added to the view hierarchy
  • viewDidAppear: Called when the view controller has been added to the view hierarchy
  • didReceiveMemoryWarning (probably won’t need this!)
  • viewWillDisappear: Used for committing editing changes and hiding the keyboard
  • viewDidDisappear: Used for stop services like audio

Model View Controller (MVC)

The model view controller design pattern is the best way to compartementalize 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

Be sure to refer to Max’s CoinFlip project to see an example of proper MVC!

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

For your mini projects you’ll need to create a new repository at github.com and add your code to this repository via the terminal. Generally, you will be

Here’s a good Git Guide that is used by CS61B: https://sp18.datastructur.es/materials/guides/using-git.html (Read Section A - D)

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.