UIAlertController操作不起作用?

问题描述:

我已经添加了一个UIAlterController到我的视图。 IT触发很好,看起来很好,并且有它的动作,但是点击Cancel动作什么也不做,它不运行任何代码,即使我添加了一个块,它也不会执行它,它似乎并不认可点击。UIAlertController操作不起作用?

代码对我来说看起来不错,所以还有其他一些原因吗?

- (void)connectionFailed:(NSString *)title { 
NSString *message = nil; 
if ([title isEqualToString:NSLocalizedString(@"Connection Refused", @"connection Refused")]) { 
    message = NSLocalizedString(@"You do not have permission to use this console", @"incorrect permissions message"); 
} 
else { 
    message = NSLocalizedString(@"Connection Failed", @"connection failed error message"); 
} 

dispatch_async(dispatch_get_main_queue(), ^{ 
    UIAlertController *alertController = [UIAlertController 
           alertControllerWithTitle:title 
           message:message 
           preferredStyle:UIAlertControllerStyleAlert]; 

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" 
            style:UIAlertActionStyleDefault 
            handler:nil]; 

    [alertController addAction:cancelAction]; 
    [self presentViewController:alertController animated:YES completion:nil]; 
}); 

编辑:它出现的问题是,以查看漏水造成的问题,不知道修复的,但这里是在调试故障:

debugging image of view stack

+0

你试过,如果用户按下取消按钮做一些事情正确 –

+0

你把这段代码放在哪里? “});”最后看起来很奇怪...... –

+0

用整个func更新了OP的更广范围 – jcad

试试这个:

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" 
            UIAlertActionStyleDefault 
            handler:nil]; 
+0

不工作我试过这第一 – jcad

使用此项:

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" 
                  style:UIAlertActionStyleCancel 
                 handler:nil]; 

变化UIAlertActionStyleDefaultUIAlertActionStyleCancel

+0

试过了,不相关的问题我害怕 – jcad

+0

此解决方案应该工作。我已经做了很多次这样的事情,它会解除警报。如果这不起作用,还有其他事情必须进行。 – tmrog

+0

同意@tmrog!我猜想还有其他一些问题。 –

试试这个:

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" 
                   style:UIAlertActionStyleCancel 
                  handler:^(UIAlertAction * _Nonnull action) { 
//your code here ... 

                  }]; 

- (void)connectionFailed:(NSString *)title { 
NSString *message = nil; 
if ([title isEqualToString:NSLocalizedString(@"Connection Refused", @"connection Refused")]) { 
    message = NSLocalizedString(@"You do not have permission to use this console", @"incorrect permissions message"); 
} 
else { 
    message = NSLocalizedString(@"Connection Failed", @"connection failed error message"); 
} 

dispatch_async(dispatch_get_main_queue(), ^{ 
    UIAlertController *alertController = [UIAlertController 
           alertControllerWithTitle:title 
           message:message 
           preferredStyle:UIAlertControllerStyleAlert]; 

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" 
            style: UIAlertActionStyleCancel 
            handler:nil]; 

    [alertController addAction:cancelAction]; 
    [self presentViewController:alertController animated:YES completion:nil]; 
}); 

刚刚编辑您的取消按钮警报风格....