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;
}
}
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With