标签:delegate ios uialertview block 宏定义
// 创建一个UIAlertView并显示出来
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:aTitle message:msg delegate:nil cancelButtonTitle:str otherButtonTitles:nil];
[alertview show];
// 创建一个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);
}
@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;
}
标签:delegate ios uialertview block 宏定义
原文地址:http://blog.csdn.net/uxyheaven/article/details/38487835