Could you explain me the correct manner to manage the UIViewController
lifecycle?
In particular, I would like to know how to use Initialize
All these commands are called automatically at the appropriate times by iOS when you load/present/hide the view controller. It's important to note that these methods are attached to UIViewController
and not to UIView
s themselves. You won't get any of these features just using a UIView
.
There's great documentation on Apple's site here. Putting in simply though:
ViewDidLoad
- Called when you create the class and load from xib. Great for initial setup and one-time-only work.
ViewWillAppear
- Called right before your view appears, good for hiding/showing fields or any operations that you want to happen every time before the view is visible. Because you might be going back and forth between views, this will be called every time your view is about to appear on the screen.
ViewDidAppear
- Called after the view appears - great place to start an animations or the loading of external data from an API.
ViewWillDisappear
/DidDisappear
- Same idea as ViewWillAppear
/ViewDidAppear
.
ViewDidUnload
/ViewDidDispose
- In Objective-C, this is where you do your clean-up and release of stuff, but this is handled automatically so not much you really need to do here.
iOS 10,11 (Swift 3.1,Swift 4.0)
According to UIViewController
in UIKit
developers,
1. loadView()
This is where subclasses should create their custom view hierarchy if they aren't using a nib. Should never be called directly.
2. loadViewIfNeeded()
Loads the view controller's view if it has not already been set.
3. viewDidLoad()
Called after the view has been loaded. For view controllers created in code, this is after -loadView. For view controllers unarchived from a nib, this is after the view is set.
4. viewWillAppear(_ animated: Bool)
Called when the view is about to made visible. Default does nothing
5. viewWillLayoutSubviews()
Called just before the view controller's view's layoutSubviews method is invoked. Subclasses can implement as necessary. Default does nothing.
6. viewDidLayoutSubviews()
Called just after the view controller's view's layoutSubviews method is invoked. Subclasses can implement as necessary. Default does nothing.
7. viewDidAppear(_ animated: Bool)
Called when the view has been fully transitioned onto the screen. Default does nothing
8. viewWillDisappear(_ animated: Bool)
Called when the view is dismissed, covered or otherwise hidden. Default does nothing
9. viewDidDisappear(_ animated: Bool)
Called after the view was dismissed, covered or otherwise hidden. Default does nothing
10. viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
Called when the view is Transitioning.
11. willMove(toParentViewController parent: UIViewController?)
12. didMove(toParentViewController parent: UIViewController?)
These two methods are public for container subclasses to call when transitioning between child controllers. If they are overridden, the overrides should ensure to call the super.
The parent argument in both of these methods is nil when a child is being removed from its parent; otherwise it is equal to the new parent view controller.
13. didReceiveMemoryWarning()
Called when the parent application receives a memory warning. On iOS 6.0 it will no longer clear the view by default.
Let's concentrate on methods, which are responsible for the UIViewController's lifecycle:
Creation:
- (void)init
- (void)initWithNibName:
View creation:
- (BOOL)isViewLoaded
- (void)loadView
- (void)viewDidLoad
- (UIView *)initWithFrame:(CGRect)frame
- (UIView *)initWithCoder:(NSCoder *)coder
Handling of view state changing:
- (void)viewDidLoad
- (void)viewWillAppear:(BOOL)animated
- (void)viewDidAppear:(BOOL)animated
- (void)viewWillDisappear:(BOOL)animated
- (void)viewDidDisappear:(BOOL)animated
- (void)viewDidUnload
Memory warning handling:
- (void)didReceiveMemoryWarning
Deallocation
- (void)viewDidUnload
- (void)dealloc
For more information please take a look on UIViewController Class Reference.
Haider's answer is correct for pre-iOS 6. However, as of iOS 6 viewDidUnload and viewWillUnload are never called. The docs state: "Views are no longer purged under low-memory conditions and so this method is never called."
There's a lot of outdated and incomplete information here. For iOS 6 and newer only:
loadView
[a]viewDidLoad
[a]viewWillAppear
viewWillLayoutSubviews
is the first time bounds are finalizedviewDidLayoutSubviews
viewDidAppear
*
viewWillLayoutSubviews
[b]*
viewDidLayoutSubviews
[b]Footnotes:
(a) - If you manually nil out your view during didReceiveMemoryWarning
, loadView
and viewDidLoad
will be called again. That is, by default loadView
and viewDidLoad
only gets called once per view controller instance.
(b) May be called an additional 0 or more times.
As of iOS 6 and onward. The new diagram is as follows: