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

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:

Newer Swift versions:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

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

Older Swift versions:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    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
    }
}

Tags:

Ios

Swift