IOS 自动关闭的Alert窗口

弹出的对话框(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;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值