<b><center>將iOS系統(tǒng)UIAlertView的UIAlertViewDelegate改為Block的方式實現(xiàn)<center></b>
相信很多iOS的開發(fā)者在開發(fā)中都有過這樣的情況芬位,某個需求需要彈出框提示弃酌,然后點擊提示框的確定或取消就需要跳轉(zhuǎn)頁面或者實現(xiàn)相應(yīng)業(yè)務(wù)邏輯仆百。這個時候罗心,我遇到大多coder是像下面這樣做的:
@interface ViewController ()<UIAlertViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"確定刪除嗎?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
[alert show];
}
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
#warning TODO...
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
這樣强重,可以礼旅,完美的實現(xiàn)了需求。雖然滿足了需求俐东,但是跌穗,一個成熟的軟件,會隨著客戶的需求而不斷調(diào)整代碼虏辫,會有不同的coder來維護和擴展蚌吸。當需求是,根據(jù)server返回的一個flag值或者什么type的值來判斷點擊了確定按鈕之后的業(yè)務(wù)邏輯砌庄,這個時候羹唠,這種方式可能就不是很適合了。通常娄昆,最初級的coder可能會在接收到flag的時候?qū)懚鄠€if判斷佩微,或者switch,然后進行多個彈框萌焰,或者為alert加個tag哺眯,在引入頭文件處聲明幾個宏#define kFlagA 100 #define kFlagB 101 把這些宏給alert的tag,最后在UIAlertViewDelegate里面根據(jù)tag值來做相應(yīng)的邏輯代碼扒俯。寫完之后測試一把奶卓,還不錯,完美撼玄。過了n久夺姑,公司招了一個人來二次開發(fā),改的改需求掌猛,然后看到了這里瑟幕,先是找彈出框show的地方,然后還去理解定義的宏,看了半天只盹,才搞明白對應(yīng)的aflag該做什么辣往,bflag又是個怎么回事的情況下才會執(zhí)行... , 通常的coder是不會去改這些方法的殖卑,如果是我看到站削,我會毫不猶豫的花幾分鐘的時間把這樣的代碼給替換掉的。 說這些孵稽,都是廢話许起。相信真正有這樣體驗過的coder一定都深有體會。下面就貼出我之前封裝的一個UIAlertView,實現(xiàn)了只點擊確定的block回調(diào)菩鲜,只點擊取消的block回調(diào)园细,以及兩個點擊都通過block回調(diào),希望對初學(xué)iOS者以及理解block接校、還不知道這種方式的iOSer有所幫助猛频。
CHAlertView.h
#import <UIKit/UIKit.h>
/**
* @brief 點擊回調(diào)
*
* @param index 點擊的下標
*/
typedef void (^CHAlertViewClickedWithIndex)(NSInteger index);
/**
* @brief 點擊確定按鈕回調(diào)
*/
typedef void (^CHAlertViewOkClicked)();
/**
* @brief 點擊取消按鈕回調(diào)
*/
typedef void (^CHAlertViewCancelClicked)();
/**
* @brief 彈出框
*/
@interface CHAlertView : NSObject
/**
* @brief 彈出提示框,點擊返回點擊下標
*
* @param title 標題
* @param msg 提示信息
* @param cancelTitle 取消按鈕文字
* @param okTitle 確定按鈕文字
* @param otherTitleArray 其他按鈕文字
* @param handle 點擊回調(diào)
*/
+ (void)showCHAlertViewWithTitle:(NSString *)title message:(NSString *)msg cancleButtonTitle:(NSString *)cancelTitle okButtonTitle:(NSString *)okTitle otherButtonTitleArray:(NSArray*)otherTitleArray clickHandle:(CHAlertViewClickedWithIndex) handle;
/**
* @brief 彈出提示框只有確定和取消按鈕
*
* @param title 標題
* @param msg 提示信息
* @param cancelTitle 取消按鈕文字
* @param okTitle 確定按鈕文字
* @param okHandle 點擊確定回調(diào)
* @param cancelClickHandle 點擊取消回調(diào)
*/
+ (void)showCHAlertViewWithTitle:(NSString *)title message:(NSString *)msg cancleButtonTitle:(NSString *)cancelTitle okButtonTitle:(NSString *)okTitle okClickHandle:(CHAlertViewOkClicked)okHandle cancelClickHandle:(CHAlertViewCancelClicked)cancelClickHandle;
/**
* @brief 彈出框蛛勉,沒有回調(diào).
*
* @param title 標題
* @param msg 提示信息
* @param cancelTitle 取消按鈕的文字
* @param okTitle 確定按鈕的文字
* @param otherTitleArray 其他按鈕文字
*/
+ (void)showCHAlertViewWithTitle:(NSString *)title message:(NSString *)msg cancleButtonTitle:(NSString *)cancelTitle okButtonTitle:(NSString *)okTitle otherButtonTitleArray:(NSArray*)otherTitleArray;
@end
CHAlertView.m (實現(xiàn)文件里面方法都沒有寫注釋鹿寻,為什么?因為頭文件里面已經(jīng)寫的很詳細了诽凌。)
#import "CHAlertView.h"
#import <objc/runtime.h>
const char *KCHAlertViewIndexBlock = "CHAlertViewIndexBlock";
const char *KCHAlertViewOkBlock = "CHAlertViewOkBlock";
const char *KCHAlertViewCancelBlock = "CHAlertViewCancelBlock";
@interface UIAlertView(CHAlertView)
@property (nonatomic,copy)CHAlertViewClickedWithIndex indexBlock;
@property (nonatomic,copy)CHAlertViewOkClicked okBlock;
@property (nonatomic,copy)CHAlertViewCancelClicked cancelBlock;
@end
@implementation UIAlertView(CHAlertView)
- (void)setIndexBlock:(CHAlertViewClickedWithIndex)indexBlock{
objc_setAssociatedObject(self, KCHAlertViewIndexBlock, indexBlock, OBJC_ASSOCIATION_COPY);
}
- (CHAlertViewClickedWithIndex)indexBlock{
return objc_getAssociatedObject(self, KCHAlertViewIndexBlock);
}
- (void)setOkBlock:(CHAlertViewOkClicked)okBlock{
objc_setAssociatedObject(self, KCHAlertViewOkBlock, okBlock, OBJC_ASSOCIATION_COPY);
}
- (CHAlertViewOkClicked)okBlock{
return objc_getAssociatedObject(self, KCHAlertViewOkBlock);
}
- (void)setCancelBlock:(CHAlertViewCancelClicked)cancelBlock{
objc_setAssociatedObject(self, KCHAlertViewCancelBlock, cancelBlock, OBJC_ASSOCIATION_COPY);
}
-(CHAlertViewCancelClicked)cancelBlock{
return objc_getAssociatedObject(self, KCHAlertViewCancelBlock);
}
@end
@interface CHAlertView ()<UIAlertViewDelegate>
@end
@implementation CHAlertView
#pragma mark -
+ (void)showCHAlertViewWithTitle:(NSString *)title message:(NSString *)msg cancleButtonTitle:(NSString *)cancelTitle okButtonTitle:(NSString *)okTitle otherButtonTitleArray:(NSArray*)otherTitleArray clickHandle:(CHAlertViewClickedWithIndex) handle{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:handle ? self : nil cancelButtonTitle:cancelTitle otherButtonTitles:okTitle, nil];
if (handle) {
alert.indexBlock = handle;
}
for (NSString *otherTitle in otherTitleArray) {
[alert addButtonWithTitle:otherTitle];
}
[alert show];
}
#pragma mark -
+ (void)showCHAlertViewWithTitle:(NSString *)title message:(NSString *)msg cancleButtonTitle:(NSString *)cancelTitle okButtonTitle:(NSString *)okTitle okClickHandle:(CHAlertViewOkClicked)okHandle cancelClickHandle:(CHAlertViewCancelClicked)cancelClickHandle{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:okTitle,nil];
if (okHandle) {
alert.okBlock = okHandle;
}
if (cancelTitle) {
alert.cancelBlock = cancelClickHandle;
}
[alert show];
}
#pragma mark -
+ (void)showCHAlertViewWithTitle:(NSString *)title message:(NSString *)msg cancleButtonTitle:(NSString *)cancelTitle okButtonTitle:(NSString *)okTitle otherButtonTitleArray:(NSArray*)otherTitleArray{
[self showCHAlertViewWithTitle:title message:msg cancleButtonTitle:cancelTitle okButtonTitle:okTitle otherButtonTitleArray:otherTitleArray clickHandle:nil];
}
#pragma mark - UIAlertViewDelegate
+ (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.indexBlock) {
alertView.indexBlock(buttonIndex);
}else{
if (buttonIndex == 0) {//取消
if (alertView.cancelBlock) {
alertView.cancelBlock();
}
}else{//確定
if (alertView.okBlock) {
alertView.okBlock();
}
}
}
}
@end
調(diào)用簡單粗暴
- (void)viewDidLoad {
[super viewDidLoad];
NSString *strTitle,*strMsg,*strCancel,*strOk;
strTitle = @"提示";
strMsg = @"來呀毡熏,互相傷害呀!";
strCancel = @"不了";
strOk = @"來就來";
[CHAlertView showCHAlertViewWithTitle:strTitle message:strMsg cancleButtonTitle:strCancel okButtonTitle:strOk otherButtonTitleArray:nil];
[CHAlertView showCHAlertViewWithTitle:strTitle message:strMsg cancleButtonTitle:strCancel okButtonTitle:strOk okClickHandle:^{
#warning TODO...
} cancelClickHandle:^{
#warning TODO...
}];
[CHAlertView showCHCustomAlertViewWithTitle:strTitle message:strMsg cancleButtonTitle:strCancel okButtonTitle:strOk otherButtonTitleArray:nil clickHandle:^(NSInteger index) {
NSLog(@"%ld",index);
#warning TODO...
}];
}
(為什么要重構(gòu)侣诵?如何實施重構(gòu)痢法? 如果看到項目中有我本文開篇所用那種彈框方式的,大可封裝一個block的彈框方式來對其重構(gòu)了杜顺。)
沒有什么技術(shù)含量的干貨疯暑,歡迎分享,隨意使用哑舒。