Asking for user confirmation before leaving a view in iOS

风格不统一 提交于 2019-12-11 05:59:33

问题


I need to show an UIAlertView before a user leaves a certain view, either by tapping a 'back' navigation bar button or by tapping one of the tab items in the tab bar I have, in order to ask him for confirmation. It would be a two-button alert, a 'Cancel' one to stay in the view, and an 'Accept' one to leave. I need to do this because I have to make the user aware that unsaved changes will be lost if leaving.

I tried to do this by creating and showing the alert view in the viewWillDisappear: method:

- (void)viewWillDisappear:(BOOL)animated
{

   UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Exit", @"")
                                                    message:NSLocalizedString(@"Are you sure you want to leave? Changes will be discarded", @"")
                                                   delegate:self
                                          cancelButtonTitle:NSLocalizedString(@"Cancel", @"")
                                          otherButtonTitles:NSLocalizedString(@"Accept", @""), nil];

   [alertView show];

   [super viewWillDisappear:animated];
}

But the view is pop anyway, and the alert view is shown after that and app crashes since its delegate is the view controller that has been already pop from the navigation stack... I don't find the way to solve this scenario, can anybody help me?

Thanks!


回答1:


Showing the alert view when viewWillDissapear won't work, because the view is already dissapearing, it's on its way to be removed.

What you can do, is add yourself a custom action when the back button is pressed, then you decide what to do when the back button is pressed, you can show the alert view, and then in one of the buttons procedd to dismiss the view, something like this:

- (id)init {
    if (self = [super init]) {
    self.navigationItem.backBarButtonItem.target = self;
    self.navigationItem.backBarButtonItem.action = @selector(backButtonPressed:);
  }
    return self;
}

Then show the alert view when the back button is pressed:

-(void)backButtonPressed:(id)sender
 {
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Exit", @"") message:NSLocalizedString(@"Are you sure you want to leave? Changes will be discarded", @"") delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", @"") otherButtonTitles:NSLocalizedString(@"Accept", @""), nil];      
    [alertView show];           
}

Now, when the confirmation button in the alert view is pressed, just call:

[self.navigationController popViewControllerAnimated:YES];

Or do nothing if the user cancels




回答2:


I would be tempted to move the data manipulation you're trying to protect into a modal view controller and handle the validation on whatever action you choose to have dismiss the modal presentation. To me, that's the point of modal: something that has to be completed before interacting with the rest of the app.



来源:https://stackoverflow.com/questions/19210686/asking-for-user-confirmation-before-leaving-a-view-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!