给UIAlertView UIActionSheet 添加block支持
2014-05-06 16:44
441 查看
block是个好语法, 可偏偏 IOS 原生的UIAlertView UIActionSheet不支持block,本文将给上述个类添加block的支持,
.h文件
[objc] view
plaincopy
-(void) handlerClickedButton:(void (^)(NSInteger btnIndex))aBlock;
.m文件
[objc] view
plaincopy
-(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 委托全部重写一遍
[objc] view
plaincopy
@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
[objc] view
plaincopy
@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
调用的时候
[objc] view
plaincopy
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"title" message:@"msg" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil nil] autorelease];
[alertView handlerClickedButton:^(NSInteger btnIndex) {
NSLogD(@"%d", btnIndex);
}];
[alertView show];
其实真这样全部改造下了发先也没简化多少,ui的代码和业务逻辑的代码混在一起了,反而代码更加混乱.
需要dome的同学请自行下载吧
XYQuickDevelop
dome 在点击Something里
.h文件
[objc] view
plaincopy
-(void) handlerClickedButton:(void (^)(NSInteger btnIndex))aBlock;
.m文件
[objc] view
plaincopy
-(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 委托全部重写一遍
[objc] view
plaincopy
@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
[objc] view
plaincopy
@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
调用的时候
[objc] view
plaincopy
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"title" message:@"msg" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil nil] autorelease];
[alertView handlerClickedButton:^(NSInteger btnIndex) {
NSLogD(@"%d", btnIndex);
}];
[alertView show];
其实真这样全部改造下了发先也没简化多少,ui的代码和业务逻辑的代码混在一起了,反而代码更加混乱.
需要dome的同学请自行下载吧
XYQuickDevelop
dome 在点击Something里
相关文章推荐
- 给UIAlertView UIActionSheet 添加block支持
- 为UIAlertView添加block支持
- UIActionSheet与UIAlertView
- 2013-8-12练习[制作一个具有UIAlertView和UIActionSheet的登陆界面]
- 在iOS8.0之后的UIAlertView和UIActionSheet的新写法
- iOS8.0 之后UIAlertView和UIActionSheet的创建方式的新变化
- iOS8.0 之后UIAlertView和UIActionSheet的创建方式的新变化
- Iphone之UIAlertView和UIActionSheet
- UIAlertView,UIActionSheet, UIMenuController
- UIView、UIResponder、UIAlertView、UIActionSheet
- 如何自定义UIActionSheet(UIAlertView)中的内容
- ios-day05-05(创建弹框(对话框),UIAlertView、UIActionSheet)
- iOS8 UIAlertController 创建UIAlertView以及UIActionSheet
- IOS UIAlertController 弹框 (ios 9.0 后代替了UIAlertView弹框 和 UIActionSheet下弹框)
- 苹果开发 笔记(85)UIActionSheet 和UIAlertView 已经失效
- Iphone之UIAlertView和UIActionSheet
- iphone开发 UIActionSheet(操作表) 和UIAlertView(警告)的用法
- 【UIAlertView警报和UIActionSheet操作表】
- iOS UIAlertView和UIActionSheet的用法
- UIAlertController(UIAlertView && UIActionSheet)