Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Affecting a UINavigationBar's Back Button Method (iOS)

I have a table view that pushes to a detail view controller. From the detail view controller, when I press the 'back' button, I'd like an integer value to change. How do I edit the navigation bar back button's action programatically. The back button is automatically placed in my app because I'm using a table view so I didn't actually create the button, so I don't know how to affect it's method.

To be clear, I still want the back button to go back to the original view, but simultaneously change an integer's value. Thanks!

like image 476
MillerMedia Avatar asked Mar 03 '26 18:03

MillerMedia


2 Answers

Thanks PengOne to point me to this direction. Add the UINavigationBarDelegate in the header file and use this in the .m file:

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem     *)item
{
  //insert your back button handling logic here
  // let the pop happen
  return YES;
}     
like image 79
Christian Loncle Avatar answered Mar 05 '26 08:03

Christian Loncle


I've figured out an easy fix to this. I simply unchecked 'Shows Navigation Bar' in the Interface Builder for the UINavigationController that the Table View was contained in. Then I used a UINavigationBar to replicate the look (but be able to add and delete buttons as I pleased).

After that I just created IBAction's that I connected to the buttons and could control an integer value from there.

(P.S. The only problem with this is that there is no 'Back' button left pointing arrow shape in the XCode interface builder as many of you know. There are solutions around this that are pretty easily found if you search).

like image 44
MillerMedia Avatar answered Mar 05 '26 06:03

MillerMedia