title: Asking for user confirmation before leaving a view in iOS
body: <p>I need to show an <code>UIAlertView</code> 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.</p>
  
  <p>I tried to do this by creating and showing the alert view in the <code>viewWillDisappear:</code> method:</p>
  
  <pre><code>- (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];
  }
  </code></pre>
  
  <p>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?</p>
  
  <p>Thanks!</p>