Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tapping 2 buttons simultaneously on UIAlertView freezes app

I have this bug: if I tap both buttons simultaneously on an UIAlertView the UIAlertView delegate will not be called, and the whole screen freezes (nothing is tappable, even though the alert view is dismissed).

Has anyone seen this bug before? Is there a way to restrict UIAlertView tapping to only one button?

- (IBAction)logoutAction:(id)sender {
        self.logoutAlertView = [[UIAlertView alloc] initWithTitle:@"Logout"
                                                              message:@"Are you sure you want to logout?"
                                                             delegate:self
                                                    cancelButtonTitle:@"No"
                                                    otherButtonTitles:@"Yes", nil];
        [self.logoutAlertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if ([alertView isEqual:self.logoutAlertView]) {
        if (buttonIndex == 0) {
            NSLog(@"cancelled logout");
        } else {
            NSLog(@"user will logout");
            [self performLogout];
        }
        self.logoutAlertView.delegate = nil;
    }
}
like image 643
user1028028 Avatar asked Oct 19 '22 22:10

user1028028


1 Answers

Yes, it's possible to tap multiple buttons on a UIAlertView and the delegate methods get called for each tap. However, this should not "freeze" your app. Step through your code to find the issue.

To prevent multiple events from being handled, set the UIAlertView's delegate property to nil after processing the first one:

- (void)showAlert {
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
  [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   // Avoid further delegate calls
   alertView.delegate = nil;

   // Do something
   if (buttonIndex == alertView.cancelButtonIndex) {
     // User cancelled, do something
   } else {
     // User tapped OK, do something
   }
}
like image 93
Marcus Adams Avatar answered Oct 23 '22 17:10

Marcus Adams