2014-10-19 138 views
28

我的應用程序中的UIAlertController出現問題,現在通過內部的日期選取器遷移到iOS8。UIAlertController延遲顯示

以下是代碼。

UIAlertController *AlertView = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet]; 

UIAlertAction *ok = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) 
{ 
[AlertView dismissViewControllerAnimated:YES completion:nil]; 
}]; 

UIAlertAction *set = [UIAlertAction actionWithTitle:NSLocalizedString(@"Set to today", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) 
{ 
[self set_to_today:nil]; 
[AlertView dismissViewControllerAnimated:YES completion:nil]; 
[self.tableView reloadData]; 
}]; 

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) 
{ 
[AlertView dismissViewControllerAnimated:YES completion:nil]; 
}]; 


UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease]; 
datePicker.datePickerMode = UIDatePickerModeDate; 
[datePicker setDate:data_appo]; 
[datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged]; 

[AlertView.view addSubview:datePicker]; 
[AlertView addAction:ok]; 
[AlertView addAction:set]; 
[AlertView addAction:cancel]; 
[self.view bringSubviewToFront:datePicker]; 
[self presentViewController:AlertView animated:YES completion:nil]; 

當用戶從UITableViewController中選擇一行時,會顯示UIAlertController和Date Picker。

問題是以下幾點: 第一次用戶選擇行一切正常......但如果用戶選擇「取消」,然後再選擇detate UIAlertController需要2-3秒才能顯示.. 。這也發生在模擬器中...

我越來越瘋狂....這使得我的應用程序有一個不好的用戶體驗。

任何幫助將大幅升值 感謝

亞歷

+0

嘗試用'AlertView'替換''self''在'bringSubviewToFront'行中並在'self'上調用'dismissViewControllerAnimated:completion:'。 – Felix 2014-10-19 13:24:44

+0

用AlertView替換自己。 – user3197643 2014-10-19 14:35:11

+0

如果我打電話給「dismissViewControllerAnimated:完成:自我」我得到一個錯誤...不知道如果我編碼正確...你可以發佈示例代碼? – user3197643 2014-10-19 14:36:17

回答

72

我是有由一個UITableView選擇行提出了UIAlertController同樣的問題。第一次一切正常,然後當用戶再次觸發警報時,在警報實際出現之前有幾秒鐘的延遲。

作爲一種變通方法我用GCD:

dispatch_async(dispatch_get_main_queue(), ^{ 
     [self presentViewController:AlertView animated:YES completion:nil]; 
    }); 

這可能是因爲-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath一個錯誤是在主線程已經執行。

我提交給蘋果一個bug報告:rdar://19285091

+5

試過這個,似乎沒有解決我的問題。仍然需要3秒鐘才能顯示警報。 – ninjaneer 2015-01-05 00:28:26

+5

我看到與表格視圖完全相同的問題。謝謝,你的解決方案爲我工作。 – 2015-01-30 16:58:28

+1

還是延遲 – 2015-11-05 07:13:07

6
DispatchQueue.main.async { 
     self.present(alertView, animated: true, completion:nil) 
    } 

雨燕3.0的版本。另外,設置animated:false也解決了我的問題。

+0

作品,非常感謝。 – DeyaEldeen 2017-11-07 19:26:02