Pressing two buttons simultaneously on a UIAlertView application to freeze

I have this error: if I press both buttons at the same time on the UIAlertView , the UIAlertView delegate will not be called, and the whole screen will freeze (nothing can be displayed even if the warning is disabled).

Has anyone seen this error before? Is there a way to restrict the UIAlertView with just one click?

 - (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; } } 
+5
source share
2 answers

Yes, you can use multiple buttons in the UIAlertView, and delegate methods are called for each click. However, this should not freeze your application. Go through your code to find the problem.

To prevent multiple events from being processed, set the UIAlertView delegation property to nil after processing the first:

 - (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 } } 
+2
source

Use UIAlertController on iOS 8:

 if ([UIAlertController class]) { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Logout" message:@"Are you sure you want to logout?" preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { NSLog(@"cancelled logout"); }]]; [alert addAction:[UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSLog(@"user will logout"); [self performLogout]; }]]; [self presentViewController:alert animated:YES completion:nil]; } else { self.logoutAlertView = [[UIAlertView alloc] initWithTitle:@"Logout" message:@"Are you sure you want to logout?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; [self.logoutAlertView show]; } 
+1
source

Source: https://habr.com/ru/post/1206722/


All Articles