項(xiàng)目中許多地方用到UIAlertController榛瓮,既然蘋果在iOS8之后出現(xiàn)這個(gè)瓦盛,說明蘋果還是倡導(dǎo)使用延柠。雖然寫起來比UIAlertView好點(diǎn)祸挪,但是還是比較重的感覺。自己就給UIAlertController做了簡(jiǎn)單的封裝贞间,但是仍然有很多不足的地方匕积,拋磚引玉??
自己封裝的想法
1.參考Masnory的代碼,自己將方法的調(diào)用改為鏈?zhǔn)健?br> 2.將一些常用的UI的需求扒出來榜跌。
具體實(shí)現(xiàn)的代碼部分
/**
* title:
* message:
* leftButtonTitle:
* rightButtonTitle:
* lelfColor:
* rightColor:
* titsuerHandlerle:
* cancelHandler:
*/
- (XLAlert * (^)(NSString *title)) title;
- (XLAlert * (^)(NSString *message)) message;
- (XLAlert * (^)(NSString *sureButtonTitle)) sureButtonTitle;
- (XLAlert * (^)(NSString *cancelButtonTitle)) cancelButtonTitle;
- (XLAlert * (^)(UIColor *sureButtonColor)) sureButtonColor;
- (XLAlert * (^)(UIColor *cancleButtonColor)) cancleButtonColor;
- (XLAlert * (^)(Surehandler handler)) suerHandler;
- (XLAlert * (^)(Cancelhandler handler)) cancelHandler;
/**
* 呈現(xiàn)
*/
- (XLAlert * (^)(void))show;
使用方法
代碼部分:
Surehandler sureHandler = ^(UIAlertAction *action) {
NSLog(@"...............你點(diǎn)擊了確認(rèn)");
};
Cancelhandler cancelhandler = ^(UIAlertAction *action) {
NSLog(@"...............你點(diǎn)擊了取消");
};
XLAlert.shareAlert.
title(@"提示").
message(@"hello\n world").
style(1).
sureButtonColor([UIColor greenColor]).
cancleButtonColor([UIColor orangeColor]).
sureButtonTitle(@"ok").
cancelButtonTitle(@"不ok").
presentViewController(self).
suerHandler(sureHandler).
cancelHandler(cancelhandler).
show();
注意:
1.Surehandler、Cancelhandler是確認(rèn)取消的點(diǎn)擊處理盅粪。
可以不實(shí)現(xiàn)钓葫,默認(rèn)一個(gè)確認(rèn)按鈕。
2.show()方法是必須得.的票顾。
3.persentViewController(self)是需要呈現(xiàn)在的控制器础浮,也是必須的。
效果圖
xg.png
最后
老規(guī)矩奠骄,上代碼豆同,不足地方望指出。
代碼