tags:

views:

358

answers:

3

I wonder what is the function is called when the back button is pressed on the navigationBar.

I want to add some functionality when the button is pressed, who knows it?

Thanks in advance

A: 

Assuming you're referring to native controls, there's no way to do quite what you want, just using the built-in stuff. What you want to do is create a 'fake' back button, and stick it up in the left side of the navigation bar. Then you can set its target and action to whatever you like.

Ben Gottlieb
A: 

I suppose you're talking about the back button that automatically is added to a UINavigationBar when you push a new viewcontroller on a navigationcontroller.

The default action for the back button is to pop the current viewcontroller from the navigation stack and return to the previous viewcontroller. If you want to define a custom behaviour to the backbutton you'll have to create a new button and tie a selector to it's action property:

//Create a new barbutton with an action 
UIBarButtonItem *barbutton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
style:UIBarButtonItemStyleDone target:self action:@selector(doSomething)];

// and put the button in the nav bar
self.navigationItem.leftBarButtonItem = barbutton;

[barbutton release];

Edit:
//An example on how the doSomething method could be implemented.

-(void) doSomething
{
//Do your custom behaviour here..

//Go back to the previous viewcontroller
[self.navigationController popViewControllerAnimated:YES];
}
Mez
-1: This replaces the back button with something non-back-ish in behavior and appearance.
Alex Reynolds
This is not styled like a back button.
Alex Reynolds
I don't have xcode at my disposal for the moment, but this should do it.
Mez
No, this looks rectangular. Apple doesn't make the back bar style available.
Alex Reynolds
If you want to keep the arrow shaped form of the button, see here: http://www.hanspinckaers.com/custom-action-on-back-button-uinavigationcontroller
Mez
A: 

The functionality you want is in the UINavigationBarDelegate protocol. Implement the -navigationBar:shouldPopItem: method and set your class as the delegate of the navigation bar in question.

Noah Witherspoon
This will only work if the navigation bar is not part of a navigation controller. (If it is, you get "*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot manually set the delegate on a UINavigationBar managed by a controller.'")
JosephH