Chris Mendez in Swift, For Developers

Understanding the UIViewController lifecycle

The more I continue learning Swift for iOS development, the more I realize the importance of understanding the UIViewController lifecycle.

The UIViewController lifecycle dictates how the operating system creates, presents, hides and destroys view controllers but, as a developer, it's hard to really wrap your head around the concept because the processes is riddled in methods titled: viewDidLoad, viewWillAppear, viewDidAppear, viewWillDisappear, viewDidDisappear, viewDidUnload, and another classic didReceiveMemoryWarning. Where do you even begin?

One Look Is Worth A Thousand Re-writes

The diagram below is probably one of the best visual explainers I've seen for the UIViewController lifecycle. It explains that viewDidLoad is where the controller starts and it can only be executed once. Therefore, if I have a tabbed application and I toggle between tabs, then anytime I need to do a refresh or change something, then it's probably best I use viewWillAppear.

UIViewController Lifecycle