ViewController Life Cycle in iOS

Suryakant Sharma
5 min readNov 23, 2019

In this Article we will look view life cycle in depth 🚀

LoadView() Creates the view that the controller manages. You should never call this method directly. The view controller calls this method when its viewproperty is requested but is currently nil. This method loads or creates a view and assigns it to the view property.

If the view controller has an associated nib file, this method loads the view from the nib file. A view controller has an associated nib file if the nibName property returns a non-nil value, which occurs if the view controller was instantiated from a storyboard, if you explicitly assigned it a nib file using the init(nibName:bundle:) method, or if iOS finds a nib file in the app bundle with a name based on the view controller'€™s class name. If the view controller does not have an associated nib file, this method creates a plain UIView object instead.

If you use Interface Builder to create your views and initialize the view controller, you must not override this method.

You can override this method in order to create your views manually. If you choose to do so, assign the root view of your view hierarchy to the view property. The views you create should be unique instances and should not be shared with any other view controller object. Your custom implementation of this method should not call super.

If you want to perform any additional initialization of your views, do so in the viewDidLoad()method.

viewDidLoad() Called after the controller’€™s view is loaded into memory.This method is called after the view controller has loaded its view hierarchy into memory. This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView() method. You usually override this method to perform additional initialization on views that were loaded from nib files.

  • Right place for API for fetching data
  • Add additional customisation point for views

viewWillApear(_:) Notifies the view controller that its view is about to be added to a view hierarchy.This method is called before the view controller’€™s view is about to be added to a view hierarchy and before any animations are configured for showing the view. You can override this method to perform custom tasks associated with displaying the view. For example, you might use this method to change the orientation or style of the status bar to coordinate with the orientation or style of the view being presented.

The method viewWillAppear should be taken in the context of what is going on in your own application, and not in the context of your application being placed in the foreground when you switch back to it from another app.Means it don’t get called when app came to foreground from switching back from other app.

Note

If a view controller is presented by a view controller inside of a popover, this method is not invoked on the presenting view controller after the presented controller is dismissed.

Warning: If the view belonging to a view controller is added to a view hierarchy directly, the view controller will not receive this message. If you insert or add a view to the view hierarchy, and it has a view controller, you should send the associated view controller this message directly. Failing to send the view controller this message will prevent any associated animation from being displayed.

Let me again explain you in great detail which Apple had not. When you add other viewController view’s to current view add it in view hierarchy by establishing parent-child relationship. Otherwise viewWillApear will not get called

UIViewController *viewController = ...;
[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];

viewWillLayoutSubviews() Called to notify the view controller that its view is about to layout its subviews. When a view’€™s bounds change, the view adjusts the position of its subviews. Your view controller can override this method to make changes before the view lays out its subviews.viewWillLayoutSubviews gets called anytime your view controller's view has its bounds changed. This happens when the view is loaded, when a rotation event occurs, or when a child view controller has its size changed by its parent. (There are probably some other situations, too).This method gets also called when a layout update happens, either by changing the view’s bounds explicitly or call setNeedsLayout or layoutIfNeeded on the view to force a layout update.

viewDidLayoutSubviews() Called to notify the view controller that its view has just laid out its subviews. When the bounds change for a view controller’€™s view, the view adjusts the positions of its subviews and then the system calls this method. However, this method is called does not indicate that the individual layouts of the view’€™s subviews have been adjusted. Each subview is responsible for adjusting its own layout.

Your view controller can override this method to make changes after the view lays out its subviews. The default implementation of this method does nothing. The key here is the change to bounds. Anything dependent on the bounds that need to be done to the view must be in this method and not in viewDidLoad or viewWillAppear, because the frame and bounds for a view are not finalized until Auto Layout has done its job of laying out the main view and subviews, and this method is then called.

viewWillDisappeared(_:) Notifies the view controller that its view is about to be removed from a view hierarchy.This method is called in response to a view being removed from a view hierarchy.

viewDidDisappeared(_:) This method is called before the view is actually removed and before any animations are configured.

Subclasses can override this method and use it to commit editing changes, resign the first responder status of the view, or perform other relevant tasks. For example, you might use this method to revert changes to the orientation or style of the status bar that were made in the viewDidAppear(_:) method when the view was first presented. If you override this method, you must call super at some point in your implementation.

didReceiveMemoryWarning() Sent to the view controller when the app receives a memory warning.Your app never calls this method directly. Instead, this method is called when the system determines that the amount of available memory is low.

You can override this method to release any additional memory used by your view controller. If you do, your implementation of this method must call the super implementation at some point.

viewWillTransition(to:with) Notifies the container that the size of its view is about to change.When the interface orientation changes, UIKit calls this method on the window’s root view controller before the size changes are about to be made. The root view controller then notifies its child view controllers, propagating the message throughout the view controller hierarchy. The parameter to contains the new CGSize size of the container’s view and the parameter with contains a UIViewControllerTransitionCoordinator coordinator, an enum that describes the new orientation.

deinit() Before a view controller is removed from memory, it gets deinitialized. You usually override deinit() to clean resources that the view controller has allocated that are not freed by ARC

Resource

https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/WorkWithViewControllers.html

Thanks for reading!

--

--