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) {
              self.init(coder)
          }
          
          deinit {
              print("\(self.classForCoder) was deinit")
          }
          
          override func loadView() {
              super.loadView()
              
              lifeCycleState = viewControllerState.loadView
              
              print("\(self.classForCoder) ------> loadView")
          }
          
          override func viewDidLoad() {
              super.viewDidLoad()
              
              lifeCycleState = viewControllerState.viewDidLoad
              
              print("\(self.classForCoder) ------> viewDidLoad")
          }
          
          override func viewWillAppear(_ animated: Bool) {
              super.viewWillAppear(animated)
              
              lifeCycleState = viewControllerState.viewWillAppear
              
              print("\(self.classForCoder) ------> viewWillAppear")
          }
          
          override func viewDidAppear(_ animated: Bool) {
              super.viewDidAppear(animated)
              
              lifeCycleState = viewControllerState.viewDidAppear
              
              print("\(self.classForCoder) ------> viewDidAppear")
          }
          
          override func viewWillDisappear(_ animated: Bool) {
              super.viewWillDisappear(animated)
              
              lifeCycleState = viewControllerState.viewWillDisappear
              
              print("\(self.classForCoder) ------> viewWillDisappear")
          }
          
          override func viewDidDisappear(_ animated: Bool) {
              super.viewDidDisappear(animated)
              
              lifeCycleState = viewControllerState.viewDidDisappear
              
              print("\(self.classForCoder) ------> viewDidDisappear")
          }
      }
        

That's it! Have fun and #CompileSwift.