TheNotSoWise
TheNotSoWise

Reputation: 899

Detecting that back was pressed to get to current view controller

I am using the navigation controller to go back from one view to previous view using the code below.

ChildViewController.swift:

self.navigationController.popViewControllerAnimated(true)

I need a way to detect that the navigation controller went to the previous view in the actual previous view like below.

ParentViewController.swift:

func backWasPressed(viewControllerIdentifier: String!) {
    // if back was pressed from this view controller and not from any other view
    if viewControllerIdentifier == "ChildViewController" {
            // do stuff here
    }
}

Is there anyway to do this?

Upvotes: 0

Views: 541

Answers (2)

matt
matt

Reputation: 535138

You don't need to know this. You may think you do, but you don't. This entire proposed architecture is specious:

func backWasPressed(viewControllerIdentifier: String!) {
    // if back was pressed from this view controller and not from any other view
    if viewControllerIdentifier == "ChildViewController" {
            // do stuff here
    }
}

If a pushed view controller has some info to communicate to a view controller further down the stack, that is the job of the pushed view controller when it is popped. It knows it is being popped, and it knows how to access the other view controller (and you can use a delegate architecture if there's any doubt about that), so the problem is properly solved in that way. It's exactly the same as when a presented view controller needs to communicate back to its presenter at dismissal time.

Upvotes: 1

Chris Wagner
Chris Wagner

Reputation: 21003

Take a look at UINavigationControllerDelegate

Upvotes: 1

Related Questions