LifeCycle ViewController in Swift October 3, 2016

Managing ViewController LifeCycle is important in iOS Development, and a good first step is to maintain uniform class inheretance patterns. A strategy I would recommend is to subclass UIViewController with a LifeCycleViewController, and then build out all your ViewControllers throughout the App to inheret from the LifeCycleViewController. Throughout the App you will have uniform logging as Views load, appear, etc., and will give you a clear picture of the Application state as it moves through its lifecycle.

            import UIKit

      class LifeCycleViewController: UIViewController {
          enum viewControllerState {
              case initializing
              case loadView
              case viewDidLoad
              case viewWillAppear
              case viewDidAppear
              case viewWillDisappear
              case viewDidDisappear
          var lifeCycleState: viewControllerState
          init(_ coder: NSCoder? = nil) {
              lifeCycleState = viewControllerState.initializing
              if let coder = coder {
                  super.init(coder: coder)!
              } else {
                  super.init(nibName: nil, bundle:nil)
          required convenience init(coder: NSCoder) {
          deinit {
              print("\(self.classForCoder) was deinit")
          override func loadView() {
              lifeCycleState = viewControllerState.loadView
              print("\(self.classForCoder) ------> loadView")
          override func viewDidLoad() {
              lifeCycleState = viewControllerState.viewDidLoad
              print("\(self.classForCoder) ------> viewDidLoad")
          override func viewWillAppear(_ animated: Bool) {
              lifeCycleState = viewControllerState.viewWillAppear
              print("\(self.classForCoder) ------> viewWillAppear")
          override func viewDidAppear(_ animated: Bool) {
              lifeCycleState = viewControllerState.viewDidAppear
              print("\(self.classForCoder) ------> viewDidAppear")
          override func viewWillDisappear(_ animated: Bool) {
              lifeCycleState = viewControllerState.viewWillDisappear
              print("\(self.classForCoder) ------> viewWillDisappear")
          override func viewDidDisappear(_ animated: Bool) {
              lifeCycleState = viewControllerState.viewDidDisappear
              print("\(self.classForCoder) ------> viewDidDisappear")

That's it! Have fun and #CompileSwift.