在UIAlertView上同时点击2个按钮会冻结应用程序

use*_*028 5 freeze uialertview ios

我有这样的错误:如果我同时挖掘上的两个按钮UIAlertViewUIAlertView代表将不会被调用,并且整个屏幕冻结(没有什么是可点击,即使警报视图被罚下场).

有没有人见过这个bug?有没有办法限制UIAlertView只有一个按钮?

- (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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ams 2

是的,可以点击 UIAlertView 上的多个按钮,并且每次点击都会调用委托方法。但是,这不应该“冻结”您的应用程序。单步调试您的代码以查找问题。

为了防止处理多个事件,请在处理第一个事件后将 UIAlertView 的 delegate 属性设置为 nil:

- (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
   }
}
Run Code Online (Sandbox Code Playgroud)