Overriding UINav Controller back button

I have a UINavigationController that pushes new views on the stack. In one view the user has the option to save data to a PLIST before exiting if data exists. When the user presses the back button I check to see if there is data in an array in the -(void)viewWillDissapear: Method.

If there is data, I present a UIAlert to ask the user what to do but the UIAlert pause, or override the the NavController to stop it from going back to the last view.

I only want to present the option to save as they are exiting the view controller. OK to save or Cancel to not save the data? IS UIAlert not the thing to use, or should this go into a different method that will pause the removal of the current view on the stack?

Compose, type, cancel. It presents an alert at the bottom of the screen.

How does it work?

Compose is presented modally. Thus it doesn't have a default back button. Instead, it has a cancel button. You have to program cancel buttons yourself in a modal view. Thus you can make it so when the cancel button is pushed, it presents a warning before calling to its delegate that it's time for it to be dismissed.

That is what I was looking for. Have not created a Modal View yet so this will be a good learning excessive.

So I would still present this from the (void)viewWillDissapear method, yes? It will override, or put on hold the dismissal of the View from the stack until the user decided to save or not save the data. In the modal view I just create a OK button that will then execute the code to save the data. Or cancel will just close the modal window allowing the window to be dismissed.

That is what I was looking for. Have not created a Modal View yet so this will be a good learning excessive.

So I would still present this from the (void)viewWillDissapear method, yes? It will override, or put on hold the dismissal of the View from the stack until the user decided to save or not save the data. In the modal view I just create a OK button that will then execute the code to save the data. Or cancel will just close the modal window allowing the window to be dismissed.

That sounds about right I think?

Click to expand...

I'm not certain of this as I haven't used a modal view in nearly a year, but I think the best way to do this is going to be more along the lines of:

Make a new .xib file. Drop in a nav bar. Drop in a cancel button. Hook up the cancel button to a cancel IBAction. In that, check if the data has been changed, and if it has been, pop up that warning (I forget what the warning that pops up from the bottom of the screen is called. Maybe a UIActionSheet or something like that?) If it hasn't been, just tell the delegate to dismiss. If the user tells the warning they don't care, tell the delegate to dismiss. If the user tells the warning they do care, then dismiss the warning and don't tell the delegate anything.

The delegate controls when the modal view appears / disappears. The view's controller just requests to be dismissed by the delegate. Why? I can't actually remember why... I remember one of the Stanford lectures covered it a few years ago.

MacRumors attracts a broad audience
of both consumers and professionals interested in
the latest technologies and products. We also boast an active community focused on
purchasing decisions and technical aspects of the iPhone, iPod, iPad, and Mac platforms.