2015-04-23 49 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