以编程方式关闭 UIAlertView



我需要有关以编程方式关闭UIAlertView的帮助。目前我有这个

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

然后后来我称之为

[alert1 dismissWithClickedButtonIndex:0 animated:NO];

但什么也没发生。

你需要设置两件事。

1. 包括您的 .h 文件:<UIAlertViewDelegate>

2.请按照以下实施方式进行操作...

   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];

关闭方法将是...

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

我希望这对你有帮助。

我也遇到了这个问题。就我而言,出于某种原因调用:

[alert dismissWithClickedButtonIndex:0 animated:NO];

并不总是有效(是的,甚至在 UI 线程上调用它,是的,警报 != nil),而是简单地将动画标志设置为 YES 它起作用了:

[alert dismissWithClickedButtonIndex:0 animated:YES];

也许这是一个苹果虫...

你应该先显示它:

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

然后在委托方法中

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

您调用的方法是正确的。
我想当您调用该方法时,alert1 为零 dismissWithClickedButtonIndex:animated:
尝试检查变量警报1。

您可以使用委托方法 -alertView:didDismissWithButtonIndex: 相反,一旦警报视图从屏幕中删除,它就会被调用,或者更好的方法是,使用后台线程,例如使用 -performSelectorInBackground:withObject:,来处理您需要执行的任何处理。

最新更新