13
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"tittle"
               message:@""
                 delegate:self
              cancelButtonTitle:@""
              otherButtonTitles:nil];
  [alertView show];
  [alertView release];

-dismissWithClickedButtonIndex:animated: method我想在它显示一段时间后关闭alerview,但是当alerview没有按钮时,如果我调用它就不起作用performSelector:withObject:afterDelay: -还有其他方法可以关闭它吗?感谢您的任何想法!

4

4 回答 4

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

就是这样。我修好了

于 2010-11-16T09:25:10.520 回答
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
    }];
});
于 2016-06-09T10:35:29.767 回答
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);
 });
于 2016-02-16T12:23:23.220 回答
0

更新到上面的答案,就像UIAlertViewdeprecated 这个链接上的答案一样

第二个功能应该是这样的

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

  [alertView dismissViewControllerAnimated:YES completion:nil];
}
于 2016-04-06T07:30:14.797 回答