views:

253

answers:

2

I have a UINavigationController with a root view controller and then I push a UIViewController onto the navigation controller's stack. When the user taps the backBarButtonItem I'd like to be able to have an alert view pop up if there are certain conditions met and cancel the pop of the view controller. For example, the user can make certain selections but some combination of them may be invalid so I want to notify them to make changes.

I know that I can prevent the user from making an invalid combination or have an alert view pop up when the invalid combination is selected but I'd rather not do that. The user may be changing selections and may be aware that a certain combination is invalid but I'd rather let them select something that makes the combination invalid then go change something else (and notify them if they haven't made changes before trying to go to the previous screen). For example, if I prevent them from selecting something that makes an invalid combination then they may have to scroll up on a screen, change something, then scroll back down instead of making a selection then scrolling up and changing something.

Using viewWillDisappear: doesn't work because, although I can produce an alert view, I cannot figure out a way to prevent the pop from occurring. The alert view displays but the view controller still pops and they are back to the root view controller (with the alert view displaying).

Is there a way to prevent the pop from occurring? If not, is this something worth filing a bug report about or is this unnecessary and/or esoteric?

+1  A: 

This sounds like something more appropriate for a Modal View Controller than it is for a View Controller on a Navigation stack.

If you're married to doing it on the stack though, it'd be nice if you could do this with UINavigationControllerDelegate, but you can't.

Is it possible to set the Back button to disabled, until the entries are valid? Perhaps when the user tries to enter something, but it's invalid, near the top of the View you have a Label animate into place with red text that tells the user they need to fix it. Meanwhile the back button is disabled and it's re-enabled after they make corrections.

Or get really creative with how your UI controls work to ensure that the user can never enter bad data.

bpapa
Actually, I'm already in a modal view controller that has a navigation controller as its root view. Then, I push the view controller that is the one I'm referring to that has a back bar button. I'm going to think about the back button disabling. I wouldn't want to ONLY disable the button (so I like your idea of having some type of notification as well) because it can be frustrating to the user if they don't know WHY they can't do something.
yabada
A: 

Let them go back, just don't save anything unless it's completely valid. That's typically the approach Apple takes.

Jamie Pinkham
The problem with this is that it could just appear like my app doesn't work properly. If the user makes selections that I don't save and then goes to the previous screen, she probably won't know why the changes weren't saved and blame my app.
yabada