弹出的对话框(UIAlertView)不需要用户点击按钮,可以自动关闭消失,诀窍是利用NSTimer定时器来关闭alertview。
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:[@"一个可以自动关闭的Alert窗口"
delegate:nil
cancelButtonTitle:nil //NSLocalizedString(@"OK", @"OK") //取消任何按钮
otherButtonTitles:nil];
//[alert setBounds:CGRectMake(alert.bounds.origin.x, alert.bounds.origin.y, alert.bounds.size.width, alert.bounds.size.height+30.0)];
[alert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
// Adjust the indicator so it is up a few pixels from the bottom of the alert
indicator.center = CGPointMake(alert.bounds.size.width/2, alert.bounds.size.height-40.0);
[indicator startAnimating];
[alert insertSubview:indicator atIndex:0];
[indicator release];
[NSTimer scheduledTimerWithTimeInterval:3.0f
target:self
selector:@selector(dismissAlert:)
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:alert, @"alert", @"testing ", @"key" ,nil] //如果不用传递参数,那么可以将此项设置为nil.
repeats:NO];
NSLog(@"release alert");
[alert release];
-(void) dismissAlert:(NSTimer *)timer{
NSLog(@"release timer");
NSLog([[timer userInfo] objectForKey:@"key"]);
UIAlertView *alert = [[timer userInfo] objectForKey:@"alert"];
[alert dismissWithClickedButtonIndex:0 animated:YES];
}
//定时器停止使用:
[timer invalidate];
timer = nil;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:[@"一个可以自动关闭的Alert窗口"
delegate:nil
cancelButtonTitle:nil //NSLocalizedString(@"OK", @"OK") //取消任何按钮
otherButtonTitles:nil];
//[alert setBounds:CGRectMake(alert.bounds.origin.x, alert.bounds.origin.y, alert.bounds.size.width, alert.bounds.size.height+30.0)];
[alert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
// Adjust the indicator so it is up a few pixels from the bottom of the alert
indicator.center = CGPointMake(alert.bounds.size.width/2, alert.bounds.size.height-40.0);
[indicator startAnimating];
[alert insertSubview:indicator atIndex:0];
[indicator release];
[NSTimer scheduledTimerWithTimeInterval:3.0f
target:self
selector:@selector(dismissAlert:)
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:alert, @"alert", @"testing ", @"key" ,nil] //如果不用传递参数,那么可以将此项设置为nil.
repeats:NO];
NSLog(@"release alert");
[alert release];
-(void) dismissAlert:(NSTimer *)timer{
NSLog(@"release timer");
NSLog([[timer userInfo] objectForKey:@"key"]);
UIAlertView *alert = [[timer userInfo] objectForKey:@"alert"];
[alert dismissWithClickedButtonIndex:0 animated:YES];
}
//定时器停止使用:
[timer invalidate];
timer = nil;