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];
});
编辑:它出现的问题是,以查看漏水造成的问题,不知道修复的,但这里是在调试故障:
答
试试这个:
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
UIAlertActionStyleDefault
handler:nil];
+0
不工作我试过这第一 – jcad
答
试试这个:
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];
});
刚刚编辑您的取消按钮警报风格....
你试过,如果用户按下取消按钮做一些事情正确 –
你把这段代码放在哪里? “});”最后看起来很奇怪...... –
用整个func更新了OP的更广范围 – jcad