2

Does anyone know of a good tutorial that explains in depth the view controller life cycle. I have read the docs so please do not link me to them. I am just looking for a practical explanation of each function such as viewDidLoad and viewWillAppear, viewWillLayoutSubviews, etc, and when best to use them with examples in Swift. If there are no tutorials would anyone be willing to explain them here in their answer.

technoY2K
  • 2,442
  • 1
  • 24
  • 38
  • Does this help? http://stackoverflow.com/questions/22214843/ios-7-difference-between-viewdidload-and-viewdidappear – Bigman May 28 '15 at 18:56

3 Answers3

7

I show the code for you with using swift.

import UIKit

class LifeCycleViewController: UIViewController {
    // MARK: -property
    lazy var testBtn: UIButton! = {
        var btn: UIButton = UIButton()
        btn.backgroundColor = UIColor.redColor()
        return btn
    }()

    // MARK: -life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        println("View has loaded")
        // set the superView backgroudColor
        self.view.backgroundColor = UIColor.blueColor()
        // add testBtn to the superView
        self.view.addSubview(self.testBtn)
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        println("View will appear")
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        println("View has appeared")
    }
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        println("View will disappear")
    }
    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        println("View has desappeared")
    }
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        println("SubViews will layout")
        // layout subViews 
        self.testBtn.frame = CGRectMake(100, 100, 100, 100)
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        println("SubViews has layouted")
        var testBtn_Width = self.testBtn.frame.width
        println("testBtn's width is \(testBtn_Width)")
    }


}

Here is the result:

View has loaded
View will appear
SubViews will layout
SubViews has layouted
testBtn's width is 100.0
SubViews will layout
SubViews has layouted
testBtn's width is 100.0
View has appeared

You can see the ViewController's life cycle clearly.

stardust
  • 109
  • 4
5

iOS UIViewController life cycle

Not bad example on this image.

LLIAJLbHOu
  • 1,313
  • 12
  • 17
2

This is the way I see it.

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.

ViewWill/DidDisappear - Same idea as WillAppear.

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.

iAnurag
  • 9,286
  • 3
  • 31
  • 48
  • viewDidLoad is good for one time setup of internal stuff, but is not good for one time setup of anything related to UI elements, as until they are laid out, they are not necessarily the right size/position. – Will M. May 28 '15 at 21:12