spin_eight spin_eight - 1 year ago 58
iOS Question

How to determine that UIViewController was started for the first time?

I have a ViewControllers hierarchy, with UINavigationViewController as the root.

How can I find out whether some ViewController was started for the first time or it was started as a result of unwinding of the navigation stack?

Answer Source

Assuming you wish to know if viewWillAppear: (or viewDidAppear:) is being called when the view controller is first being displayed or if it's being displayed because another view controller has been dismissed, you can easily do the following:

override func viewWillAppear(animated: Bool) {

    if isBeingPresented() || isMovingToParentViewController() {
        // This is the first time this instance of the view controller will appear
    } else {
        // This controller is appearing because another was just dismissed