您的位置:首页 > 产品设计 > UI/UE

UIAlertView自动消失

2012-06-01 18:13 441 查看
原文地址:UIAlertView自动消失作者:若水一叶


UIAlertView自动消失   

话说,在写程序的过程中用到很多提示的信息,于是非常自然地就要使用UIAlertView控件。

但是这些提示的信息有时候只需提示就行,不用操作,那么此时就要这个提示框自动消失就OK了。

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

(1)结合NSTimer

UIAlertView baseAlert = nil;

- (void) performDismiss: (NSTimer *)timer

{

 

   [baseAlertdismissWithClickedButtonIndex:0 animated:NO];//important

    [baseAlertrelease];

    baseAlert= NULL;

}     

- (void) presentSheet

{

    baseAlert= [[UIAlertView alloc] 

                              initWithTitle:@"Alert"message:@"nMessage Message Message " 

                              delegate:selfcancelButtonTitle:nil

                              otherButtonTitles:nil];

    [NSTimerscheduledTimerWithTimeInterval:2.0f target:self selector:@selector(performDismiss:)

                                  userInfo:nil repeats:NO];

    [baseAlertshow];

}

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

- (void) dimissAlert:(UIAlertView *)alert

{

    if(alert)

    {

        [alertdismissWithClickedButtonIndex:[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这里,欢迎关注~~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  class timer null