最近写程序突然冒出个奇怪的需求就是让UIAlertView自动消失,不过实现方法很简单,使用以下代码:
// hides alert sheet or popup. use this method when you need to explicitly dismiss the alert. // it does not need to be called if the user presses on a button - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;
一个小Demo代码:
@implementation ViewController { UIAlertView *alertView; } - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)alert:(id)sender { alertView = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"I will dismiss later..." delegate:nil cancelButtonTitle:@"Dismiss now" otherButtonTitles:nil, nil]; [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(dismissAlertView:) userInfo:nil repeats:NO]; [alertView show]; } - (void)dismissAlertView:(NSTimer*)timer { NSLog(@"Dismiss alert view"); [alertView dismissWithClickedButtonIndex:0 animated:YES]; } @end
点击视图中的按钮,运行alert方法:
保持程序前台运行3秒,对话框消失:
但是在这里遇到个问题,加入对话框启动后,立即点击Home键让程序进入后台,等待大概3秒钟,对话框消失后,再打开程序,界面如下:
可以看到按钮变成黑色了,但是界面依然可以响应用户的输入。
谷歌了一下,找到一篇文章,说这是iOS 7中UIAlertView的一个Bug。
希望知道原因和解决方法的朋友告诉我一下。
通过代码让UIAlertView自动消失,布布扣,bubuko.com
原文:http://blog.csdn.net/u010962810/article/details/21245785