Affecting a UINavigationBar's Back Button Method (iOS)

二次信任 提交于 2019-12-10 10:26:34

问题


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!


回答1:


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



回答2:


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




回答3:


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, e.g. incrementing or decrementing a counter.




回答4:


You could try implementing viewDidDisappear, which should be called as the detail view controller's view goes out of view.



来源:https://stackoverflow.com/questions/8333316/affecting-a-uinavigationbars-back-button-method-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!