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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值