Navigation L3.6.2

Setting root view controller

setting root view controller

Setting initial view controller to avoid black screen on load

setting root view controller

Stacks

Navigation controllers keep track of their views in a stack.

stack cups

stack push pop

From the settings app if we select the notifications tab then the messages tab the stack will end up looking like this with the messages view as the topViewController

settings stack

Every time the OS deallocates a swift class including a view controller it calls a de-initializer called deinit. This method is only available to swift classes.

In the case of a view controller deinit is called when it is slide off the screen to the right.

class ViewController: UIViewController {

    {...}

    deinit {
        print("view controller deallocated")
    }
}

Pushing Detail View From Code

There are roughly 5 steps the table view controller takes to push a detail view onto the stack with code.

  1. Receives the invocation that a row has been selected and gets the index of that row with tableView(_, didSelectRowAt).
  2. Get the detail view controller from the storyboard object.
  3. Looks up the object associated with the index path that was passed in to the table view (allVillains[indexPath.row]).
  4. Sets the data in the detail view controller (detailController.villain)
  5. And finally in the last line of code pushes the detail view controller onto the navigation stack.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let detailController = storyboard!.instantiateViewController(withIdentifier: "VillainDetailViewController") as! VillainDetailViewController

    detailController.villain = allVillains[indexPath.row]

    navigationController!.pushViewController(detailController, animated: true)
}