UIAlertController动作不起作用?

时间:2017-05-04 11:07:25

标签: ios objective-c uialertcontroller

我在视图中添加了一个UIAlterController。 IT触发器很好,看起来很好,并且有它的动作,但是点击取消操作什么都不做,它不运行任何代码,即使我添加一个它不会执行它的块,它似乎根本不识别点击。

代码对我来说很好,还有其他原因吗?

<div>

编辑:看来问题是查看泄漏导致问题,不确定修复但是这里是调试中的细分:

debugging image of view stack

4 个答案:

答案 0 :(得分:1)

使用此:

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

UIAlertActionStyleDefault更改为UIAlertActionStyleCancel

答案 1 :(得分:1)

试试这个:

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

                                                             }];

答案 2 :(得分:0)

试试这个:

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

答案 3 :(得分:0)

- (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];
});

刚编辑了取消按钮提醒样式....