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

i want to dimiss the alerview after it's showing for some time,but when the alertview has no button,it doesn't work if i invoked -dismissWithClickedButtonIndex:animated: methodand-performSelector:withObject:afterDelay: is there any other way to dismiss it ? thanks for any ideas!

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ben
  • 1,020
  • 1
  • 15
  • 27

4 Answers4

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

that's it.i fix it

ben
  • 1,020
  • 1
  • 15
  • 27
  • 2
    Ben, if the cancelButton of the alertView is set to "nil", how will the "[alertView dismissWithClickedButtonIndex:0 animated:YES];" thing work??? I tried that and it worked, but cant figure out how.... Any help is appreciated! Thank you! – Balaram Sep 20 '11 at 12:46
2

Use 10 seconds delays to dismiss

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
    }];
});
Draken
  • 3,134
  • 13
  • 34
  • 54
1

In Xamarin.iOS / Monotouch this worked for me:

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

Remember that if an async method is called from a background thread (not the main UI thread) then InvokeOnMainThread would still be required. This just means that you call the above method like this:

 BeginInvokeOnMainThread(() =>
 {
   ShowToast(message);
 });
Daniele D.
  • 2,624
  • 3
  • 36
  • 42
0

update to the answer above as UIAlertView is deprecated Answer At this Link

the 2nd function should be like this

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

  [alertView dismissViewControllerAnimated:YES completion:nil];
}
Community
  • 1
  • 1