Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to handle "back" navigation button action in iOS

I need to detect when the user taps the "back" button on the navigation bar, in order to perform some operations when that occurs. I'm trying to set manually an action to such button, this way:

[self.navigationItem.backBarButtonItem setAction:@selector(performBackNavigation:)];  - (void)performBackNavigation:(id)sender {    // Do operations     [self.navigationController popViewControllerAnimated:NO]; } 

I firstly placed that code in the view controller itself, but I found that self.navigationItem.backBarButtonItem seemed to be nil, so I moved that same code to the parent view controller, which pushes the former to the navigation stack. But I'm neither able to make it work. I've read some posts regarding this issue, and some of them said that the selector needs to be set at the parent view controller, but for me it doesn't work anyway... What could I'm doing wrong?

Thanks

like image 481
AppsDev Avatar asked Sep 16 '13 09:09

AppsDev


People also ask

How do I customize the back button on my iPhone?

Turn on Back Tap Check that you have the latest version of iOS on your iPhone 8 or later. Go to Settings > Accessibility > Touch, and tap Back Tap. Tap Double Tap or Triple Tap and choose an action. Double or triple tap on the back of your iPhone to trigger the action you set.

How can remove back button in iOS?

To hide the back button on navigation bar we'll have to either set the navigation button as nil and then hide it or hide it directly. Let's create a project, add 2 view controller and Embed them in navigation controller.

What is navigation Controller in iOS?

The navigation controller manages the navigation bar at the top of the interface and an optional toolbar at the bottom of the interface. The navigation bar is always present and is managed by the navigation controller itself, which updates the navigation bar using the content provided by its child view controllers.


1 Answers

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     } } 
like image 132
Kumar KL Avatar answered Oct 16 '22 14:10

Kumar KL