2010-11-16 64 views
13
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"tittle" 
       message:@"" 
       delegate:self 
       cancelButtonTitle:@"" 
       otherButtonTitles:nil]; 
    [alertView show]; 
    [alertView release]; 

我想dimiss的alerview它顯示了一段時間後,但是當alertview沒有按鈕,如果我調用-dismissWithClickedButtonIndex:animated: method和 - performSelector:withObject:afterDelay: 是否有任何其他的方式來關閉它它不工作? 感謝任何想法!有沒有辦法否認了一段時間後,沒有任何按鍵UIAlertView中?

回答

26
-(void)xx { 
    [self performSelector:@selector(dismissAlertView:) withObject:alertView afterDelay:2]; 
} 
-(void)dismissAlertView:(UIAlertView *)alertView{ 
    [alertView dismissWithClickedButtonIndex:0 animated:YES]; 
} 

這就是它。我修復它

+3

不錯的工作....但我建議你寫的文章或使自己的博客 – Azhar 2011-08-01 09:06:56

+2

本,如果alertView的cancelButton設置爲「無」,怎麼會「[alertView dismissWithClickedButtonIndex:0動畫:是] ;」事情工作?我試過,它的工作,但無法弄清楚如何....任何幫助表示讚賞!謝謝! – Balaram 2011-09-20 12:46:39

+0

精彩真的great..tnx – 2015-08-10 06:31:45

1

在Xamarin.iOS/MonoTouch的這個工作對我來說:

private async Task ShowToast(string message, UIAlertView toast = null) 
    { 
     if (null == toast) 
     { 
      toast = new UIAlertView(null, message, null, null, null); 
      toast.Show(); 
      await Task.Delay(2000); 
      await ShowToast(message, toast); 
      return; 
     } 

     UIView.BeginAnimations(""); 
     toast.Alpha = 0; 
     UIView.CommitAnimations(); 
     toast.DismissWithClickedButtonIndex(0, true); 
    } 

請記住,如果異步方法是從後臺線程(不是叫主UI線程),那麼InvokeOnMainThread仍然是必需的。 這只是意味着你撥打上面的方法是這樣的:

BeginInvokeOnMainThread(() => 
{ 
    ShowToast(message); 
}); 
0

更新答案上面UIAlertViewdeprecated Answer At this Link

的第2個功能應該是這樣的

-(void)dismissAlertView:(UIAlertController *)alertView{ 

    [alertView dismissViewControllerAnimated:YES completion:nil]; 
} 
2

使用10秒延遲駁回

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleAlert]; 
[self presentViewController:alertController animated:YES completion:nil]; 

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
    [alertController dismissViewControllerAnimated:YES completion:^{ 
     p\Perform Action after dismiss alertViewController 
    }]; 
}); 
相關問題