dismissing a UIAlertView programmatically

Nick P picture Nick P · Sep 14, 2012 · Viewed 26.6k times · Source

I need help on dismissing a UIAlertView programmatically. Currently I have this

UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];

then later on I call this

[alert1 dismissWithClickedButtonIndex:0 animated:NO];

but nothing happens.

Answer

Arun picture Arun · Sep 14, 2012

You need to set two things.

1. include your .h file : <UIAlertViewDelegate>

2. please follow below implementation...

   UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil]; 
        [alert1 show];
        [self performSelector:@selector(dismiss:) withObject:alert1 afterDelay:1.0];

the dismiss method will be...

-(void)dismiss:(UIAlertView*)alert
{
    [alert dismissWithClickedButtonIndex:0 animated:YES];
}

I hope this will help you.