- The View of a View Controller
- Setting the Initial View Controller
- Tab Bar Controllers
- Loaded and Appearing Views
- Interacting with View Controllers and Their Views
- Bronze Challenge: Another Tab
- Silver Challenge: Different Background Colors
Interacting with View Controllers and Their Views
Let’s look at some methods that are called during the lifecycle of a view controller and its view. Some of these methods you have already seen, and some are new.
- init(coder:) is the initializer for UIViewController instances created from a storyboard.
When a view controller instance is created from a storyboard, its init(coder:) is called once. You will learn more about this method in Chapter 13.
init(nibName:bundle:) is the designated initializer for UIViewController.
When a view controller instance is created without the use of a storyboard, its init(nibName:bundle:) is called once. Note that in some apps, you may end up creating several instances of the same view controller class. This method is called once on each view controller as it is created.
loadView() is overridden to create a view controller’s view programmatically.
viewDidLoad() is overridden to configure views created by loading an interface file. This method is called after the view of a view controller is created.
viewWillAppear(_:) is overridden to configure the view controller’s view each time it appears on screen.
This method and viewDidAppear(_:) are called every time your view controller is moved onscreen. viewWillDisappear(_:) and viewDidDisappear(_:) are called every time your view controller is moved offscreen.
To preserve the benefits of lazy loading, you should never access the view property of a view controller in init(nibName:bundle:) or init(coder:). Asking for the view in the initializer will cause the view controller to load its view prematurely.