首页 > 代码库 > UIAlertView使用
UIAlertView使用
UIAlertView使用
基本用法
// 创建一个UIAlertView并显示出来
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:aTitle message:msg delegate:nil cancelButtonTitle:str otherButtonTitles:nil];
[alertview show];
delegate
// 创建一个UIAlertView并显示出来
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:aTitle message:msg delegate:self cancelButtonTitle:str otherButtonTitles:nil];
[alertview show];
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLogD(@"%ld", (long)buttonIndex);
}
block封装
@interface UIAlertView (XY)
typedef void(^UIAlertView_block_self_index)(UIAlertView *alertView, NSInteger btnIndex);
-(void) handlerClickedButton:(UIAlertView_block_self_index)aBlock{
self.delegate = self;
objc_setAssociatedObject(self, UIAlertView_key_clicked, aBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
UIAlertView_block_self_index block = objc_getAssociatedObject(self, UIAlertView_key_clicked);
if (block) block(alertView, buttonIndex);
}
// 使用
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:aTitle message:msg delegate:nil cancelButtonTitle:str otherButtonTitles:nil];
[alertView handlerClickedButton:^(UIAlertView *alertView, NSInteger btnIndex) {
NSLogD(@"%ld", (long)btnIndex);
}];
[alertview show];
额外
@interface UIAlertView (XY)
// 延时消失
-(void) showWithDuration:(NSTimeInterval)i{
[NSTimer scheduledTimerWithTimeInterval:i
target:self
selector:@selector(xyDismiss)
userInfo:self
repeats:NO];
[self show];
}
-(void) xyDismiss{
[self dismissWithClickedButtonIndex:0 animated:YES];
}
// 简化调用的宏
#define SHOWMSG(title, msg, cancel) [XYCommon showAlertViewTitle:title message:msg cancelButtonTitle:cancel];
+(void) showAlertViewTitle:(NSString *)aTitle message:(NSString *)msg cancelButtonTitle:(NSString *)str{
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:aTitle message:msg delegate:nil cancelButtonTitle:str otherButtonTitles:nil];
[alertview show];
}
@interface NSObject (XY)
// 简化调用的方法
-(UIAlertView *) showMessage:(BOOL)isShow title:(NSString *)aTitle message:(NSString *)aMessage cancelButtonTitle:(NSString *)aCancel otherButtonTitles:(NSString *)otherTitles, ... NS_REQUIRES_NIL_TERMINATION{
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:aTitle message:aMessage delegate:nil cancelButtonTitle:aCancel otherButtonTitles:nil];
va_list args;
va_start(args, otherTitles);
if (otherTitles)
{
[alter addButtonWithTitle:otherTitles];
NSString *otherString;
while ((otherString = va_arg(args, NSString *)))
{
[alter addButtonWithTitle:otherString];
}
}
va_end(args);
if (isShow) [alter show];
return alter;
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。