简体   繁体   中英

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!

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;
}     

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.

(PS 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).

If you're using a UINavigationController , then UINavigationBarDelegate is the delegate class and it implements -navigationBar:shouldPopItem . You can put the action you want to trigger in that method, eg incrementing or decrementing a counter.

您可以尝试实现viewDidDisappear,应该在详细视图控制器的视图不在视图时调用它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM