UIAlertView自动消失

原文地址:UIAlertView自动消失 作者:若水一叶

UIAlertView自动消失   

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

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

(1)结合NSTimer

UIAlertView baseAlert = nil;
- (void) performDismiss: (NSTimer *)timer
{
 
      [baseAlertdismissWithClickedButton Index:0 animated:NO];//important
        [baseAlertrelease];
        baseAlert= NULL;
       
- (void) presentSheet
{
        baseAlert= [[UIAlertView alloc] 
                                                            initWithTitle:@"Alert"message:@"nMessage Message Message " 
                                                            delegate:selfcancelButtonTitle:nil
                                                            otherButtonTitles:nil];
        [NSTimerscheduledTimerWithTimeIn terval:2.0f target:self selector:@selector(performDismiss:)
                                                                    userInfo:nil repeats:NO];
        [baseAlertshow];
}


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

- (void) dimissAlert:(UIAlertView *)alert
{
        if(alert)
        {
                [alertdismissWithClickedButton Index:[alert cancelButtonIndex]animated:YES];
                [alertrelease];
        }
}

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

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

[loadingAlertView dismissWithClickedButtonIndex:0animated: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(180, 15, 80, 80);
 
[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这里,欢迎关注~~~


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值