Dopamine
Dopamine

Reputation: 79

How to find out if the back-button was tapped in the UINavigationBar?

Firstly I have tried to manage it with the viewWillDisapper-method, but this not detailed enough for me. Is their an other solution?

Also tried the delegate:

- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item

but nothing happens.

Upvotes: 0

Views: 1174

Answers (3)

William Remacle
William Remacle

Reputation: 1470

You need to change the default back button, in viewDidLoad :

- (void) viewDidLoad
{
   self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back"
                                           style:UIBarButtonItemStyleBordered
                                           target:self
                                           action:@selector(handleBack:)] autorelease];
}

And of course you have to pop the controller in your method :

- (void) handleBack:(id)sender
{
    // ... your code !

    [self.navigationController popViewControllerAnimated:YES];
}

Upvotes: 2

Danra
Danra

Reputation: 9906

"Also tried the delegate: ... but nothing happens." First thing to do is to set a breakpoint inside the function which you suspect isn't called. To set up a breakpoint just click to the left of the code in xcode.

Upvotes: 0

nevan king
nevan king

Reputation: 113747

You should try the other UINavigationBarDelegate delegate method, –navigationBar:shouldPopItem:, and return YES after doing whatever you need to do. "Should" delegate methods are called before the thing happens. "Did" methods are called after it happens.

The method that you are calling is not for the back button. The back button will "Pop" a view controller. The opposite (which you are using) is to "Push" a view controller. A push adds a new view controller to the stack. A pop removes a view controller from the stack.

Also, make sure to conform to UINavigationBarDelegate. If nothing happened with the delegate method that you used, something is set up wrong. AFAIK the delegate should be automatically set up if you are using a UINavigationController.

Upvotes: 2

Related Questions