我有这个错误:如果同时在UIAlertView上同时点击两个按钮,则不会调用UIAlertView委托(delegate),并且整个屏幕卡住(即使关闭了警报 View ,也无法点击任何内容)。

以前有没有人见过这个错误?有没有一种方法可以将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;
    }
}

最佳答案

是的,可以在UIAlertView上点击多个按钮,并且每次点击都会调用委托(delegate)方法。但是,这不应“卡住”您的应用程序。单步执行代码以查找问题。

为了防止处理多个事件,请在处理完第一个事件后将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
   }
}

关于ios - 在UIAlertView上同时轻按2个按钮将卡住应用程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26889360/

10-11 22:40
查看更多