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

win*_*ell 2 uialertview ios 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];
Run Code Online (Sandbox Code Playgroud)

Vla*_*lad 16

将警报控制器保存到某个属性(例如alertController),然后将其关闭

[self.alertController dismissViewControllerAnimated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

要通过触摸警报来执行此操作,请向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];
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述