封装简便弹出框

每次写弹出框的时候,UIAlertView的代理写得特别麻烦,而且当代码越多,找起来就比较麻烦,我用block进行了简单封装。以后遇到会重复用到的代码段,都可以像这样进行封装。

直接上代码:

LLAlertView.h
#import
#import

    typedef void(^ReturnBlock)();
    @interface LLAlertView : NSObject

    @property (nonatomic,copy)ReturnBlock cancelBlock;
    @property (nonatomic,copy)ReturnBlock tureBlock;

    + (void)creatAlertViewWithTitle:(NSString *)titleText withMessage:(NSString *)messageText withcancelTitle:(NSString *)cancelText withCancelBlock:(ReturnBlock)cancelBlock withotherTitle:(NSString *)otherText  withotherBlock:(ReturnBlock)tureBlock;

    @end

LLAlertView.m
#import "LLAlertView.h"

     static LLAlertView *__alertView;

    @implementation LLAlertView

    + (void)creatAlertViewWithTitle:(NSString *)titleText  withMessage:(NSString *)messageText withcancelTitle:(NSString *)cancelText withCancelBlock:(ReturnBlock)cancelBlock withotherTitle:(NSString *)otherText  withotherBlock:(ReturnBlock)tureBlock{

        if (nil == __alertView) {
            __alertView = [[LLAlertView alloc]init];
        }

        __alertView.cancelBlock = cancelBlock;
        __alertView.tureBlock = tureBlock;
        UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:titleText message:messageText delegate:__alertView cancelButtonTitle:cancelText otherButtonTitles:otherText, nil];
        [alertview show];

    }

    #pragma mark - AlertView Delegte
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

        switch (buttonIndex) {
            case 0:
                if (_cancelBlock) {
                    _cancelBlock();
                }
        
                break;
            case 1:
                if (_tureBlock) {
                    _tureBlock();
                }
                break;
            default:
                break;
        }

    }

Demo下载请看这里:LLAlertView

希望能帮到大家,虽然只是一个小的例子,希望能活学活用,封装出更多的东西。

你可能感兴趣的:(封装简便弹出框)