uinavigationcontroller - Asking for user confirmation before leaving a view in iOS -


i need show uialertview before user leaves view, either tapping 'back' navigation bar button or tapping 1 of tab items in tab bar have, in order ask him confirmation. two-button alert, 'cancel' 1 stay in view, , 'accept' 1 leave. need because have make user aware unsaved changes lost if leaving.

i tried creating , showing alert view in viewwilldisappear: method:

- (void)viewwilldisappear:(bool)animated {     uialertview *alertview = [[uialertview alloc] initwithtitle:nslocalizedstring(@"exit", @"")                                                     message:nslocalizedstring(@"are sure want leave? changes discarded", @"")                                                    delegate:self                                           cancelbuttontitle:nslocalizedstring(@"cancel", @"")                                           otherbuttontitles:nslocalizedstring(@"accept", @""), nil];     [alertview show];     [super viewwilldisappear:animated]; } 

but view pop anyway, , alert view shown after , app crashes since delegate view controller has been pop navigation stack... don't find way solve scenario, can me?

thanks!

showing alert view when viewwilldissapear won't work, because view dissapearing, it's on way removed.

what can do, add custom action when button pressed, decide when button pressed, can show alert view, , in 1 of buttons procedd dismiss view, this:

- (id)init {     if (self = [super init]) {     self.navigationitem.backbarbuttonitem.target = self;     self.navigationitem.backbarbuttonitem.action = @selector(backbuttonpressed:);   }     return self; } 

then show alert view when button pressed:

-(void)backbuttonpressed:(id)sender  {      uialertview *alertview = [[uialertview alloc] initwithtitle:nslocalizedstring(@"exit", @"") message:nslocalizedstring(@"are sure want leave? changes discarded", @"") delegate:self cancelbuttontitle:nslocalizedstring(@"cancel", @"") otherbuttontitles:nslocalizedstring(@"accept", @""), nil];           [alertview show];            } 

now, when confirmation button in alert view pressed, call:

[self.navigationcontroller popviewcontrolleranimated:yes]; 

or nothing if user cancels


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

html - Repeat image to extend header to fill screen -

javascript - Backbone.js getting target attribute -