How to get notified of popped view in UINavigationController?
Asked Answered
H

5

11

I want to perform a action when the user pressed the back button on my UINavigationController when arrived at a certain UIViewController.

Unfortunately it looks like UINavigationControllerDelegate doesn't have any methods to get notified of the popping of views.

As a workaround I now have in the viewDidDisappear method my action, that only gets fired when animated is YES. This works, but it's a bit ugly.

How should I do this properly?

Hackberry answered 9/3, 2011 at 13:57 Comment(1)
use viewWillDisappear for ur action if u want previously to knowTamworth
F
0

You can either call a delegate method when viewWillDisappear or set logic on viewWillAppear for certain UIViewController.

Fusilier answered 9/3, 2011 at 13:57 Comment(0)
B
31

The most popular way of handling a pop from navigation view controller (as well as from modal) is implementing viewWillDisappear for your view controller.

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    if (self.isMovingFromParentViewController || self.isBeingDismissed) {
        // This view controller is being popped or dismissed
    }
}
Benue answered 26/3, 2014 at 10:38 Comment(0)
A
5

If you have a reference to the controller down the stack, the one which will show when this one is popped, you can register as a delegate and check it through

navigationController:willShowViewController:animated:
Anstus answered 25/10, 2011 at 17:6 Comment(0)
F
0

You can either call a delegate method when viewWillDisappear or set logic on viewWillAppear for certain UIViewController.

Fusilier answered 9/3, 2011 at 13:57 Comment(0)
U
0

you can observe to the UINavigationControllerDelegate and check if transition will happened:

- (void)navigationController:(UINavigationController *)navigationController
          willShowViewController:(UIViewController *)viewController
                        animated:(BOOL)animated
    {
        if([navigationController.viewControllers containsObject:self])
        {
            NSLog(@"push");
        }
        else
        {
            NSLog(@"pop");
        }
    }
Uintathere answered 23/12, 2014 at 15:47 Comment(1)
If you have a UITabBarController, you should use this solution because the viewWillDisappear is call each time you change the current tab index.Autocratic
T
0

First you need to conform UINavigationControllerDelegate to navigation controller and implement this method

 public func navigationController(
    _ navigationController: UINavigationController,
    didShow viewController: UIViewController,
    animated: Bool) {
    guard let dismissedViewController =
            navigationController.transitionCoordinator?
            .viewController(forKey: .from),
          !navigationController.viewControllers
            .contains(dismissedViewController) else {
        return
    }
    performOnDismissed(for: dismissedViewController)
}

In performOnDismissed func, you can check dismissedViewController, like

if (dismissedViewController is DesireViewController)

then, fire your method.

Theodoretheodoric answered 13/1, 2021 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.