Xcode: detect when the navigation controller is pressed with the back button

Scenario

I have an application with a navigation controller. When the navigation controller pushes another controller onto the stack, the back button "<(name of the last view controller)" is displayed in the upper left corner of the screen.

What I need

I need something like (pseudo-code) ...

-(void)detectedBackButtonWasPushed { NSLog(@"Back Button Pressed"); //Do what I need done } 

Question

Since this button is created by the navigation controller, and I did not create this button in the storyboards, how can I return the back button to a method like this?

examples of what Eve tried for Oleg

 -(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"notification"]; if (viewController == vc) { NSLog(@"BACK BUTTON PRESSED"); } } 

Is that how I should do it? The reason for this is not working.

+5
source share
2 answers

Use viewWillDisappear to discover this.

 -(void) viewWillDisappear:(BOOL)animated { if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) { [self backButtonPressed]; [self.navigationController popViewControllerAnimated:NO]; } [super viewWillDisappear:animated]; } -(void)backButtonPressed { NSLog(@"YEA"); } 
+4
source

I previously solved this by setting LeftBeart to navigate as the back button with a custom selector that rejects the view along with what else needs to be done.

I could also suggest looking at the back button element and adding a target: “I,” invoked by touch.

0
source

Source: https://habr.com/ru/post/1202802/


All Articles