tags:

views:

5462

answers:

2

Hi,

I want to prevent a view from exiting when the users hits the top left navigation bar button. If I can do this, I can then ask them if they want to save their work or discard it.

I tried this. But it does not give me the pretty arrow on the left hand side, instead it just gives me a button.

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];

I also tried.

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];

But that did not work. I did not see a way to override the selector of this either? Anyone else had any luck.

Cheers, John.

+1  A: 

By using the target and action variables that you are currently leaving 'nil', you should be able to wire your save-dialogs in so that they are called when the button is "selected". Watch out, this may get triggered at strange moments.

I agree mostly with Amagrammer, but I don't think it would be that hard to make the button with the arrow custom. I would just rename the back button, take a screen shot, photoshop the button size needed, and have that be the image on the top of your button.

TahoeWolverine
I agree you could photoshop and I think I might do this if I really wanted it but now have decided to change the look and feel a tiny bit to get this to work the way I want.
John Ballinger
Yes, except that the actions are not triggered when they are attached to the backBarButtonItem. I don't know if this is a bug or a feature; it's possible that even Apple doesn't know.As for the photoshopping exercise, again, I would be wary that Apple would reject the app for misusing a canonical symbol.
Amagrammer
+9  A: 

Unlike Amagrammer said, it's possible. You have to subclass your navigationController. I explained everything here (including example code): http://www.hanspinckaers.com/custom-action-on-back-button-uinavigationcontroller

HansPinckaers
Hey Hans, thanks for sharing. Nice result.
John Ballinger
Apple's documentation (http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference.html) says that "This class is not intended for subclassing". Though I'm not sure what they mean by this - they could mean "you shouldn't normally need to do that", or they could mean "we will reject your app if you mess with our controller"...
Psionides
This is certainly the only way to do it. Wish I could award you more points Hans!
Adam Eberbach
Can you actually prevent a view from exiting using this method? What would you make the popViewControllerAnimated method return if you wanted the view not to exit?
JosephH
Yeah, you can. Just don't call the superclass method in your implementation, be aware! You shouldn't do that, the user expects to go back in the navigation. What you can do is ask for an confirmation. According to Apples documentation popViewController returns: "The view controller that was popped from the stack." So when nothing is popped your should return nil;
HansPinckaers
Apparently I was mistaken in my original response. It just goes to show, sometimes "good" answers are wrong answers...
Amagrammer