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

时间:2015-04-23 08:29:03

标签: 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];

1 个答案:

答案 0 :(得分:16)

将警报控制器保存到某个属性(例如alertController)并使用

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

要通过触摸警报来添加轻触手势识别器,以警告控制器的视图:

- (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