Trying to handle "back" navigation button action in iOS Trying to handle "back" navigation button action in iOS ios ios

Trying to handle "back" navigation button action in iOS


Try this code using VIewWillDisappear method to detect the press of The back button of NavigationItem:

-(void) viewWillDisappear:(BOOL)animated{    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound)     {        // Navigation button was pressed. Do some stuff         [self.navigationController popViewControllerAnimated:NO];    }    [super viewWillDisappear:animated];}

OR There is another way to get Action of the Navigation BAck button.

Create Custom button for UINavigationItem of back button .

For Ex:

In ViewDidLoad :

- (void)viewDidLoad {    [super viewDidLoad];    UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStyleBordered target:self action:@selector(home:)];    self.navigationItem.leftBarButtonItem=newBackButton;}-(void)home:(UIBarButtonItem *)sender {    [self.navigationController popToRootViewControllerAnimated:YES];}

Swift :

override func willMoveToParentViewController(parent: UIViewController?) {    if parent == nil     {        // Back btn Event handler    }}


Swift

override func didMoveToParentViewController(parent: UIViewController?) {    if parent == nil {        //"Back pressed"    }}


Perhaps this answers doesn't fit your explanation but question title. It's useful when you are trying to know when you tapped the back button on an UINavigationBar.

In this case you can use UINavigationBarDelegate protocol and implement one of this methods:

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item;- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item;

When didPopItem method is called, it's because you either tapped the back button or you used [UINavigationBar popNavigationItemAnimated:] method and the navigation bar did pop the item.

Now, if you want to know what action triggered the didPopItem method you can use a flag.

With this approach I don't need to manually add a left bar button item with an arrow image in order to make it similar to iOS back button, and be able to set my custom target/action.


Let's see an example:

I have a view controller that has a page view controller, and a custom page indicator view. I'm also using a custom UINavigationBar to display a title to know on what page am I and the back button to go back to the previous page. And I also can swipe to previous/next page on page controller.

#pragma mark - UIPageViewController Delegate Methods- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {    if( completed ) {        //...        if( currentIndex > lastIndex ) {            UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"Some page title"];            [[_someViewController navigationBar] pushNavigationItem:navigationItem animated:YES];            [[_someViewController pageControl] setCurrentPage:currentIndex];        } else {            _autoPop = YES; //We pop the item automatically from code.            [[_someViewController navigationBar] popNavigationItemAnimated:YES];            [[_someViewController pageControl] setCurrentPage:currentIndex];        }    }}

So then I implement UINavigationBar delegate methods:

#pragma mark - UINavigationBar Delegate Methods- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {    if( !_autoPop ) {        //Pop by back button tap    } else {        //Pop from code    }    _autoPop = NO;    return YES;}

In this case I used shouldPopItem because the pop is animated and I wanted to handle the back button immediately and not to wait until transition is finished.