View Controller Lifecycle
Exaplain View Controller Lifecycle
View Controller comes with a set of methods that manage its view hierarchy. iOS automatically calls these methods at appropriate times when a view controller transitions between states.
View Management
awakeFromNib() - It is called when the associated nib file with a class is loaded.
viewDidLoad() — This method is called after the view controller has loaded its view hierarchy into memory.
viewWillAppear() — Called just before the view controller’s content view is added to the app’s view hierarchy.
viewWillLayoutSubviews() - Called to notify the view controller that its view is about to layout its subviews.
viewDidLayoutSubviews() - Called to notify the view controller that its view has just laid out its subviews.
viewDidAppear() — Called just after the view controller’s content view has been added to the app’s view hierarchy.
viewWillDisappear() — Called just before the view controller’s content view is removed from the app’s view hierarchy.
viewDidDisappear() — Called just after the view controller’s content view has been removed from the app’s view hierarchy.
View Controller and View
There are three ways to specifies view controller's view. It is lazily created the first time the system or any code accesses the controller’s view property.
- Storyboard
- Nib file
- loadView() method
What's the difference between storyboard and nib file?
- A nib file lets you specify the views of a single view controller.
- The nib file also stores only minimal information about the view controller itself.
- Storyboard - Information about segues and relationships between view controllers.
Explain ViewController Initializer: initWithNibName vs initWithCoder
- initWithNibName is only used when the view controller is created directly in code
- initWithCoder is called when view controller is created from storyboard, deserialization
Storyboard vs Nib vs Code
- Storyboards: A visual tool for laying out multiple application views and the transitions between them.
- NIBs (or XIBs): Each NIB file corresponds to a single view element and can be laid out in the Interface Builder
View Controller's main responsibility
- Updating the contents of the views, usually in response to changes to the underlying data.
- Responding to user interactions with views.
- Resizing views and managing the layout of the overall interface.