我使用 UIAlertController 和 UIActivityIndicatorView 作为我的加载指示器,它会关闭,直到我的应用程序获得服务器响应。但是当我的服务器或网络出现问题时,我的应用程序永远不会得到响应,我无法从 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];
}
关于ios - 如何在没有按钮的情况下关闭 UIAlertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29817807/