UIAlertController不一样的用法
2018.01.30
Mr.deng
 热度
℃
项目中许多地方用到UIAlertController,既然苹果在iOS8之后出现这个,说明苹果还是倡导使用。虽然写起来比UIAlertView好点,但是还是比较重的感觉。自己就给UIAlertController做了简单的封装,但是仍然有很多不足的地方,抛砖引玉😀
自己封装的想法
1.参考Masnory的代码,自己将方法的调用改为链式。
2.将一些常用的UI的需求扒出来。
具体实现的代码部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| /** * 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;
|
1 2 3 4
| /** * 呈现 */ - (XLAlert * (^)(void))show;
|
使用方法
代码部分:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| Surehandler sureHandler = ^(UIAlertAction *action) { NSLog(@"...............你点击了确认"); }; Cancelhandler cancelhandler = ^(UIAlertAction *action) { NSLog(@"...............你点击了取消"); };
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是确认取消的点击处理。
可以不实现,默认一个确认按钮。
2.show()方法是必须得.的。
3.persentViewController(self)是需要呈现在的控制器,也是必须的。
效果图
最后
老规矩,上代码,不足地方望指出。
代码