打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
UIAlertView自动消失

UIAlertView自动消失   

话说,在写程序的过程中用到很多提示的信息,于是非常自然地就要使用UIAlertView控件。
但是这些提示的信息有时候只需提示就行,不用操作,那么此时就要这个提示框自动消失就OK了。

UIAlertView弹出后2s让其自动消失,两种方法:

(1)结合NSTimer

UIAlertView baseAlert = nil;
- (void) performDismiss: (NSTimer *)timer
{
    [baseAlert dismissWithClickedButtonIndex:0 animated:NO];//important
    [baseAlert release];
    baseAlert = NULL;
    
- (void) presentSheet
{
    baseAlert = [[UIAlertView alloc] 
                              initWithTitle:@"Alert" message:@"\nMessage Message Message " 
                              delegate:self cancelButtonTitle:nil
                              otherButtonTitles: nil];
    [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector: @selector(performDismiss:)
                                   userInfo:nil repeats:NO];
    [baseAlert show];
}


(2)使用PerformSelector:withObject:afterDelay:方法

- (void) dimissAlert:(UIAlertView *)alert
{
    if(alert)
    {
        [alert dismissWithClickedButtonIndex:[alert cancelButtonIndex] animated:YES];
        [alert release];
    }
}

- (void)showAlert{            
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil 
cancelButtonTitle:nil otherButtonTitles:nil];
    
    [alert show];
    [self performSelector:@selector(dimissAlert:) withObject:alert afterDelay:2.0];
}

很多人以为UIAlertView是要点击取消或其他按钮才可以消失的,其实不是,可以用代码触发这个动作~

[loadingAlertView dismissWithClickedButtonIndex:0 animated:YES];

 

动态修改当前的UIAlertView的对象,其实方法很简单,取到它改就成咯~我是自定义了一个LoadingAlertView继承自UIAlertView。然后进行相关操作。

在生成LoadingAlertView的时候,如果你想修改UIAlertView内的布局,就在下面的方法修改。

- (void)layoutSubviews {   [super layoutSubviews];   //你要写的内容   }

如果你想添加一些自己的空间,比如UIImageView,UISwitch等,可以写在初始化方法里面,我是这样做的~

- (id)initWithFrame:(CGRect)frame {   if (self = [super initWithFrame:frame]) {   UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];   activity.frame = CGRectMake(180158080);   [activity startAnimating];   [activity sizeToFit];   [self addSubview:activity];   }   return self;   }

如果需要动态修改,就写一个方法,让外部来调用,如下~

方法:

- (void)loaded {   for (id obj in self.subviews) {   if ([obj isKindOfClass:[UILabel class]]) {   UILabel *label = obj;   label.text = @"加载完毕";   label.textAlignment = UITextAlignmentCenter;   }   else if ([obj isKindOfClass:[UIActivityIndicatorView class]]) {   [obj stopAnimating];   }   }   }

调用:

[loadingAlertView loaded];

下面看效果图吧~

代码在https://github.com/juxuechen/AlertPickerView/tree/dismissAlert 这里,欢迎关注~~~

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
浅析ios开发中Block块语法的妙用
自己整理的ios app实现自动升级
GCD中UIAlertView的写法
objc_setAssociatedObject与Block的简单使用
为 NSArray/NSDictionary 优雅地过滤 nil 值 | 吴品诚的技术博客
软件测试技术之iOS 单元测试—逻辑测试
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服