2015-04-23 216 views
2

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

坠毁,当我触摸屏幕,下面是我的代码:

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]; 
+0

你怎么做网络操作?发布一些代码。 –

+0

你可以这样做 [alertView dismissWithClickedButtonIndex:0 animated:YES]; – aBilal17

回答

14

您的报警控制器保存到某些属性(例如,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]; 
} 

enter image description here

+0

当我触摸屏幕时,它在控制台中显示“发送到实例的无法识别的选择器”,是否有错? – winhell

+0

我很确定这不是建议。大多数时候,建议你有某种按钮,说“确定”或“取消”等。 – erdekhayser

+0

谢谢!非常感谢和有用。 – Felipe