Setting root view controller
Setting initial view controller to avoid black screen on load
Stacks
Navigation controllers keep track of their views in a stack.
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
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.
- Receives the invocation that a row has been selected and gets the index of that row with
tableView(_, didSelectRowAt)
. - Get the detail view controller from the
storyboard
object. - Looks up the object associated with the index path that was passed in to the table view (
allVillains[indexPath.row]
). - Sets the data in the detail view controller (
detailController.villain
) - 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)
}