码迷,mamicode.com
首页 > 其他好文 > 详细

UIAlertView使用

时间:2014-08-11 10:07:51      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:delegate   ios   uialertview   block   宏定义   

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;
}

UIAlertView使用,布布扣,bubuko.com

UIAlertView使用

标签:delegate   ios   uialertview   block   宏定义   

原文地址:http://blog.csdn.net/uxyheaven/article/details/38487835

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!