dismissing a UIAlertView programmatically

假如想象 提交于 2019-12-03 10:54:10
Arun

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.

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...

Mil0R3

you should display it first:

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

then in delegate method

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
    if(buttonIndex==0){
     // do something
    }
}

The methods you called is correct.
I guess the alert1 is nil when your call the method dismissWithClickedButtonIndex:animated:
Try to check your variable alert1.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!