Yoel Jimenez del valle
Yoel Jimenez del valle

Reputation: 1278

NavigationController.topViewController is equal to viewController in navigationController delegate

I am implementing the navigationControllerDelegate func:

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
    print("callin the Navigation Controller Delgate")

    if viewController === self {
        print("Calling the Navigation Controller delegate because is self and going to call tapButton")
        //want to know who was previously on top of navigation.
    }
}

I want to know here which was the viewController that is being removed form the stack since apple docs says that the

viewController The view controller whose view and navigation item properties are being shown.

This means that this assumption is true:

viewController == navigationController.topViewController

or this one:

viewController == navigationController.visibleViewController

If not then one of this are the the viewControllers that is going to be removed. It's not cleat for me since the func parameter name is willShow viewController, or is just a fancy name and the will show is the already shown.

So if not how from the delegate I may know which VC is being removed from the Navigation Stack.

Upvotes: 0

Views: 384

Answers (2)

Cenk Bilgen
Cenk Bilgen

Reputation: 1445

It would have been nice if the delegate provided the viewController being removed, but I found it's just straight forward just to keep a reference.

weak var lastRemovedViewController: UIViewController? = nil

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
    defer { lastRemovedViewController = viewController }
    print("Navigation Removed \(lastRemovedViewController.debugDescription)")
}

Upvotes: 2

Yoel Jimenez del valle
Yoel Jimenez del valle

Reputation: 1278

yes is the answer, the viewController passed in the delegate func is the same as the topViewController, also is equal to navigationController.visibleViewController. so there is no way to know which was the removed viewController from the stack.

Upvotes: 0

Related Questions