ios - 如何在没有按钮的情况下关闭 UIAlertController

标签 ios uialertview uialertcontroller

我使用 UIAlertController 和 UIActivityIndi​​catorView 作为我的加载指示器,它会关闭,直到我的应用程序获得服务器响应。但是当我的服务器或网络出现问题时,我的应用程序永远不会得到响应,我无法从 AlertView 取回我的控制权,我想知道是否有某种方法可以通过触摸屏幕关闭此 AlertView。 我的 UIAlertView 没有标题和任何按钮。 任何提示将不胜感激。

当我触摸屏幕时它崩溃了,这是我的代码:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
                                                        message:@"正在装载页面...\n\n"
                                                        preferredStyle:UIAlertControllerStyleAlert];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.center = CGPointMake(130.5, 65.6);
spinner.color = [UIColor darkGrayColor];
[spinner startAnimating];
[alert.view addSubview:spinner];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]initWithTarget:alert action:@selector(stopWaiting)];
[alert.view addGestureRecognizer:recognizer];

最佳答案

将您的警报 Controller 保存到某个属性(例如,alertController)并使用

关闭它
[self.alertController dismissViewControllerAnimated:YES completion:nil];

要通过触摸警报来做到这一点,请将点击手势识别器添加到 alertController 的 View 中:

- (void)showAlert {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@""
                                                                             message:@"Some message"
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                           action:@selector(closeAlert)];
    [alertController.view addGestureRecognizer:tapGestureRecognizer];
    self.alertController = alertController;

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

- (void)closeAlert {
    [self.alertController dismissViewControllerAnimated:YES
                                             completion:nil];
}

enter image description here

关于ios - 如何在没有按钮的情况下关闭 UIAlertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29817807/

相关文章:

ios - 应用程序崩溃,但没有设备日志

ios - 我可以在 Xcode 4.1 中使用 Storyboard吗?

ios - 键盘未显示

尝试添加文本字段时 iOS 8.3 UIAlertController 崩溃

ios - UITableViewCell setEditing 截断标签

iphone - iPhone-如何在运行时处理错误

ios - UIAlertView 和 UILocalNotifications 一样有 fireDate 吗?

ios - UIAlertController 出现在奇怪的位置

ios - UIPopover contentSize 在运行时发生变化

ios - NSArray valueForKey ObjectAtIndex