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

tôi muốn dimiss các alerview sau khi nó được hiển thị cho một thời gian, nhưng khi alertview không có nút, nó không hoạt động nếu tôi gọi -dismissWithClickedButtonIndex:animated: method và- performSelector:withObject:afterDelay: là có cách nào khác để bỏ qua nó? cảm ơn mọi ý tưởng!Có cách nào để loại bỏ một nút UIalertView không sau một thời gian không?

Trả lời

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

đó là it.I sửa chữa nó

+3

làm việc tốt đẹp .... nhưng tôi sẽ đề nghị bạn viết bài hoặc tạo blog của riêng bạn – Azhar

+2

Ben, nếu nút hủy của alertView được đặt thành "không", làm thế nào sẽ "[alertView dismissWithClickedButtonIndex: 0 animated: YES] ; " công việc gì ??? Tôi đã cố gắng và nó đã làm việc, nhưng không thể tìm ra cách .... Bất kỳ trợ giúp được đánh giá cao! Cảm ơn bạn! – Balaram

+0

tuyệt vời thực sự là một tuyệt vời..tnx –

1

Trong Xamarin.iOS/MonoTouch này làm việc cho tôi:

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); 
    } 

Hãy nhớ rằng nếu một phương pháp async được gọi từ một sợi nền (không phải là chủ đề giao diện người dùng chính) thì InvokeOnMainThread vẫn sẽ được yêu cầu. này chỉ có nghĩa là bạn gọi phương thức trên như thế này:

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

cập nhật cho câu trả lời ở trên như UIAlertViewdeprecated Answer At this Link

chức năng thứ 2 nên được như thế

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

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

này Dùng 10 giây chậm trễ để loại bỏ

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 
    }]; 
}); 
Các vấn đề liên quan