views:

49

answers:

3

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.

+1  A: 

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];
}
William Remacle
Don't forget to release that button item. Otherwise, you're leaking it.
Peter Hosey
Thx. Is there a possibility to set the button back to the arrow-look?
Dopamine
-1, this will leak. Create it like this:`UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(handleBack:)];`, set it like this: `self.navigationItem.leftBarButtonItem = button;` and **remember to release it**, `[button release]`.
Emil
Indeed, sorry guy, I usually put autorelease in alloc.
William Remacle
+2  A: 

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.

nevan
Thank you! I have a delegate-conform UINavigationController, but none the delegate-methods works for my app. This is strange.
Dopamine
Then the first thing to do is find out why your delegate methods aren't being called. Copy and paste the method names from the docs. Use the delegate methods in a simpler app to see how they work. Look through your code and read about how to make delegates work.
nevan
A: 

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

Danra