给UIAlertView UIActionSheet 添加block支持
2013-10-18 00:23
274 查看
block是个好语法, 可偏偏 IOS 原生的UIAlertView UIActionSheet不支持block,本文将给上述个类添加block的支持,
.h文件
.m文件
按照这种思路将UIAlertView,UIActionSheet 委托全部重写一遍
调用的时候
其实真这样全部改造下了发先也没简化多少,ui的代码和业务逻辑的代码混在一起了,反而代码更加混乱.
需要dome的同学请自行下载吧
XYQuickDevelop
dome 在点击Something里
.h文件
-(void) handlerClickedButton:(void (^)(NSInteger btnIndex))aBlock;
.m文件
-(void) handlerClickedButton:(void (^)(NSInteger btnIndex))aBlock{ self.delegate = self; objc_setAssociatedObject(self, UIActionSheet_key_clicked, aBlock, OBJC_ASSOCIATION_COPY); } -(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ void (^block)(NSInteger btnIndex) = objc_getAssociatedObject(self, UIActionSheet_key_clicked); if (block) block(buttonIndex); }
按照这种思路将UIAlertView,UIActionSheet 委托全部重写一遍
@interface UIAlertView (Block) -(void) handlerClickedButton:(void (^)(NSInteger btnIndex))aBlock; -(void) handlerCancel:(void (^)(void))aBlock; -(void) handlerWillPresent:(void (^)(void))aBlock; -(void) handlerDidPresent:(void (^)(void))aBlock; -(void) handlerWillDismiss:(void (^)(NSInteger btnIndex))aBlock; -(void) handlerDidDismiss:(void (^)(NSInteger btnIndex))aBlock; -(void) handlerShouldEnableFirstOtherButton:(BOOL (^)(void))aBlock; @end
@interface UIActionSheet (Block) -(void) handlerClickedButton:(void (^)(NSInteger btnIndex))aBlock; -(void) handlerCancel:(void (^)(void))aBlock; -(void) handlerWillPresent:(void (^)(void))aBlock; -(void) handlerDidPresent:(void (^)(void))aBlock; -(void) handlerWillDismiss:(void (^)(void))aBlock; -(void) handlerDidDismiss:(void (^)(NSInteger btnIndex))aBlock; @end
调用的时候
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"title" message:@"msg" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil] autorelease]; [alertView handlerClickedButton:^(NSInteger btnIndex) { NSLogD(@"%d", btnIndex); }]; [alertView show];
其实真这样全部改造下了发先也没简化多少,ui的代码和业务逻辑的代码混在一起了,反而代码更加混乱.
需要dome的同学请自行下载吧
XYQuickDevelop
dome 在点击Something里
相关文章推荐
- 给UIAlertView UIActionSheet 添加block支持
- 为UIAlertView添加block支持
- UIAlertView、UIActionSheet兼容iOS8
- 【iPhone代码片段之一】UIActionSheet&UIAlertView的应用
- UIAlertController UIAlertAction (UIAlertView, UIActionSheet)(最好)
- UIActionSheet,UIAlertView,UISlider,UIProgressView,UISwitch,UISegmentControl,UIActivityIndicatorView基
- IOS UIAlertController 弹框 (ios 9.0 后代替了UIAlertView弹框 和 UIActionSheet下弹框)
- 使用第三方的支持Block 的UIAlertView
- iOS开发6:UIActionSheet与UIAlertView
- IOS复习UIActionSheet&UIAlertView
- UIAlertView和UIActionSheet
- IOS UIAlertController 弹框 (ios 9.0 后代替了UIAlertView弹框 和 UIActionSheet下弹框)
- ios-消息弹框之UIAlertView, UIActionSheet以及UIAlertController小结
- iOS开发 对话框UIActionSheet和UIAlertView用法
- Swift UIAlertView/UIActionSheet
- UI控件篇——UIActionSheet(操作表)和UIAlertView(警告框)
- UIAlertView及UIActionSheet 在ios8极其以下版本的兼容问题解决方案
- 【IOS 开发学习总结-OC-47】★ios开发之UI控件——UIAlertView与 UIActionSheet
- UIActionSheet与UIAlertView的简单实用
- 【学习ios之路:UI系列】UIAlertView. UIActionSheet 和 UIAlertController