自封装UIAlert类:BlockAlertView

.h文件

typedef void (^blockAlertViewCallBackBlock)(int btnIndex);


#import <Foundation/Foundation.h>

@interface BlockAlertView : NSObject


- (void)showAlertWithTitle:(NSString *)title msg:(NSString *)msg callbackBlock:(blockAlertViewCallBackBlock)block
         cancelButtonTitle:(NSString *)cancelBtnTitle otherButtonTitles:(NSString *)otherButtonTitles, ... ;

@end


.m文件

#import "BlockAlertView.h"

@implementation BlockAlertView
{
    blockAlertViewCallBackBlock _callbackBlock;
}

- (void)dealloc
{
    [_callbackBlock release];
    _callbackBlock = nil;
    
    [super dealloc];
}

- (void)showAlertWithTitle:(NSString *)title msg:(NSString *)msg callbackBlock:(blockAlertViewCallBackBlock)block
     cancelButtonTitle:(NSString *)cancelBtnTitle otherButtonTitles:(NSString *)otherButtonTitles, ... ;
{
    if (!block)
    {
        return;
    }
    
    _callbackBlock = nil;
        //强引用
    _callbackBlock = [block copy];
    
    [self retain];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:cancelBtnTitle otherButtonTitles:nil];
    if (otherButtonTitles)
    {
        [alert addButtonWithTitle:otherButtonTitles];
        va_list args ;
        va_start(args, otherButtonTitles);
        NSString *title = nil;
        while ((title = va_arg(args, NSString *)))
        {
            [alert addButtonWithTitle:title];
        }
        va_end(args);
    }
    
    [alert show];
    [alert release];
    alert = nil;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    _callbackBlock(buttonIndex);
    
    [self release];
}


@end

BlockAlertView类的使用方法:

BlockAlertView *blockAlert = [BlockAlertView new];
                         [blockAlert showAlertWithTitle:@"标题" msg:@"内容" callbackBlock:^(int btnIndex){
                             
                             if (btnIndex == 0)
                             {
                                 return ;
                             }
                             
                         } cancelButtonTitle:@"确定" otherButtonTitles: nil];
                         
                         [blockAlert release];


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值