原文地址:UIAlertView自动消失
作者:若水一叶
UIAlertView自动消失
话说,在写程序的过程中用到很多提示的信息,于是非常自然地就要使用UIAlertView控件。
但是这些提示的信息有时候只需提示就行,不用操作,那么此时就要这个提示框自动消失就OK了。
UIAlertView弹出后2s让其自动消失,两种方法:
(1)结合NSTimer
UIAlertView baseAlert = nil;
- (void) performDismiss: (NSTimer *)timer
{
}
- (void) presentSheet
{
}
(2)使用PerformSelector:withObject:afterDelay:方法
- (void) dimissAlert:(UIAlertView *)alert
{
}
-(void)showAlert{
cancelButtonTitle:nil otherButtonTitles:nil];
}
很多人以为UIAlertView是要点击取消或其他按钮才可以消失的,其实不是,可以用代码触发这个动作~
[loadingAlertView dismissWithClickedButton
动态修改当前的UIAlertView的对象,其实方法很简单,取到它改就成咯~我是自定义了一个LoadingAlertView继承自UIAlertView。然后进行相关操作。
在生成LoadingAlertView的时候,如果你想修改UIAlertView内的布局,就在下面的方法修改。
- (void)layoutSubviews {[super layoutSubviews]; //你要写的内容 }
如果你想添加一些自己的空间,比如UIImageView,UISwitch等,可以写在初始化方法里面,我是这样做的~
- (id)initWithFrame:(CGRect)frame {if (self = [super initWithFrame:frame]) { UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithActivityIndicato rStyle:UIActivityIndicatorViewS tyleWhite]; 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这里,欢迎关注~~~