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

How can I close UIAlertController without button

我使用 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];

将您的警报控制器保存到一些 属性(例如,alertController)并使用

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

要通过触摸警报来完成,请将点击手势识别器添加到 alertController 的视图:

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