Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dismissing a UIAlertView programmatically

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.

like image 706
Nick P Avatar asked Sep 14 '12 00:09

Nick P


People also ask

How do I dismiss Uialertview programmatically?

You can use the delegate method -alertView:didDismissWithButtonIndex: instead—it gets called once the alert view's been removed from the screen, OR better approach is , use a background thread, e.g. with -performSelectorInBackground:withObject:, to handle whatever processing you need to do.

How do I dismiss an alert?

1. From the Alerts tab, under Alerts, click Inbox. 2. On the Alerts Inbox page, click the check boxes in front of the alerts you want to dismiss, then click the Dismiss button.

How do I dismiss an alert in Swift?

You can dismiss the alert by calling dismissViewControllerAnimated method on alertController object.

How do you dismiss an alert with click on outside of the alert IOS?

Step 1 − Open Xcode and create a single view application and name it UIAlertSample. So basically, when we tap on the button an alert will be displayed, when the user taps outside the alert the alert will be dismissing.


2 Answers

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.

like image 160
Arun Avatar answered Sep 28 '22 12:09

Arun


I encountered this problem too. In my case, for some reason calling:

[alert dismissWithClickedButtonIndex:0 animated:NO];

didn't work always (yes, even calling it on UI thread and yes, alert != nil), instead simply setting the animated flag to YES it worked:

[alert dismissWithClickedButtonIndex:0 animated:YES];

Maybe it's an Apple bug...

like image 40
Eugenio Avatar answered Sep 28 '22 10:09

Eugenio