Objective-C Runtime之着魔的UIAlertView
2016-04-14 11:47
441 查看
原文出处: ian博客(@ianisme )
Objective-C
我们要给每个UIAlertView赋值一个tag值,在delegate方法中还要进行tag的判断以及buttonIndex的判断,太繁琐了。
创建一个UIAlertView的Category
UIAlertView+ActionBlock.h
Objective-C
UIAlertView+ActionBlock.m
Objective-C
在主类中取消delegate,使用block属性
Objective-C
我们通过使用Category给UIAlertView扩展了一个block属性,当block被设置后就会调用setCallBack方法,触发self.delegate = self,即主类中的UIAlertView的delegate方法被Category中的方法覆盖。这样不仅有效解决问题,还解决了其他人修改该类的安全性(block被去掉后,原delegate恢复)
如下不给tag值为1003的UIAlertView设置block,即调用原delegate方法。
Objective-C
传统的UIAlertView:
在一个类中有多个UIAlertView,不同的UIAlertView对应不同的事件,我们使用的传统方法如下:Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #pragma mark - action method - (IBAction)firstButtonClick:(id)sender { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil]; alertView.tag = 1001; [alertView show]; } - (IBAction)secondButtonClick:(id)sender { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil]; alertView.tag = 1002; [alertView show]; } - (IBAction)ThirdButtonClick:(id)sender { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil]; alertView.tag = 1003; [alertView show]; } #pragma mark - delegate method - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (alertView.tag == 1001) { if (buttonIndex == 1) { NSLog(@"普通alertView1001执行ok"); } } else if (alertView.tag == 1002) { if (buttonIndex == 1) { NSLog(@"普通alertView1002执行ok"); } } else if (alertView.tag == 1003) { if (buttonIndex == 1) { NSLog(@"普通alertView1003执行ok"); } } } |
着魔的UIAlertView:
下面我们使用Category和Associated Objects进行魔法修改创建一个UIAlertView的Category
UIAlertView+ActionBlock.h
Objective-C
1 2 3 4 5 6 7 8 9 | #import <UIKit/UIKit.h> typedef void (^AlertCallBack)(UIAlertView *, NSUInteger); @interface UIAlertView (ActionBlock)<UIAlertViewDelegate> @property (nonatomic, copy) AlertCallBack callBack; @end |
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #if TARGET_IPHONE_SIMULATOR #import <objc/objc-runtime.h> #else #import <objc/runtime.h> #import <objc/message.h> #endif @implementation UIAlertView (ActionBlock) - (void)setCallBack:(AlertCallBack)callBack { objc_setAssociatedObject(self, @selector(callBack), callBack, OBJC_ASSOCIATION_COPY_NONATOMIC); self.delegate = self; } - (AlertCallBack)callBack { return objc_getAssociatedObject(self, @selector(callBack)); } #pragma mark - delegate method - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (self.callBack) { self.callBack(alertView, buttonIndex); } } |
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #pragma mark - action method - (IBAction)firstButtonClick:(id)sender { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil]; alertView.callBack = ^(UIAlertView *alertView, NSUInteger buttonIndex){ if (buttonIndex == 1) { NSLog(@"魔法alertView1001执行ok"); } }; [alertView show]; } - (IBAction)secondButtonClick:(id)sender { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil]; alertView.callBack = ^(UIAlertView *alertView, NSUInteger buttonIndex){ if (buttonIndex == 1) { NSLog(@"魔法alertView1002执行ok"); } }; [alertView show]; } - (IBAction)ThirdButtonClick:(id)sender { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil]; alertView.callBack = ^(UIAlertView *alertView, NSUInteger buttonIndex){ if (buttonIndex == 1) { NSLog(@"魔法alertView1003执行ok"); } }; [alertView show]; } |
如下不给tag值为1003的UIAlertView设置block,即调用原delegate方法。
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | - (IBAction)firstButtonClick:(id)sender { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil]; alertView.callBack = ^(UIAlertView *alertView, NSUInteger buttonIndex){ if (buttonIndex == 1) { NSLog(@"魔法alertView1001执行ok"); } }; alertView.tag = 1001; [alertView show]; } - (IBAction)secondButtonClick:(id)sender { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil]; alertView.callBack = ^(UIAlertView *alertView, NSUInteger buttonIndex){ if (buttonIndex == 1) { NSLog(@"魔法alertView1002执行ok"); } }; alertView.tag = 1002; [alertView show]; } - (IBAction)ThirdButtonClick:(id)sender { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil]; alertView.tag = 1003; [alertView show]; } - (IBAction)fourthButtonClick:(id)sender { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *alertAction){ NSLog(@"如果你是iOS8以上的应用,这个适合你,简单明了"); }]; [alertController addAction:cancelAction]; [alertController addAction:okAction]; [self presentViewController:alertController animated:YES completion:nil]; } #pragma mark - delegate method - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (alertView.tag == 1001) { if (buttonIndex == 1) { NSLog(@"普通alertView1001执行ok"); } } else if (alertView.tag == 1002) { if (buttonIndex == 1) { NSLog(@"普通alertView1002执行ok"); } } else if (alertView.tag == 1003) { if (buttonIndex == 1) { NSLog(@"普通alertView1003执行ok"); } } else if (alertView.tag == 1004) { if (buttonIndex == 1) { NSLog(@"普通alertView1004执行ok"); } } } |
相关Demo下载:
https://github.com/ianisme/UIAlertViewBYRuntime_Demo总结:
通过Associated Objects我们有效的解决了UIAlertView的繁琐问题,如果您是开发iOS8以上的应用,建议您弃用UIAlertView,苹果的UIAlertController已经有了更好的解决方案。QQ技术交流群290551701相关文章推荐
- OC学习日记Day3:数据在内存的储存形式,CPU查找内存数据
- Android Intent传递List<Object>数据
- 关于CGImageRef
- 【JavaScript】Object.prototype.toString.call()进行类型判断
- 对于AttributeError: 'Flask' object has no attribute 'cli'的解决办法
- Objective-C的属性(property)解析:
- SQL sever 中系统表sys.objects的作用与字段含义
- warning: Could not resolve external type c:objc(cs
- Objective-c学习日总结之与C的区别及类与对象的定义,类的声明及调用方法
- 面向对象高级——Object类、包装类以及匿名内部类
- org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: sys.entity.Role; nested exception is org.hibernate.PersistentObjectException: 的解决方案
- Objective-C Associated Objects 的实现原理
- 关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- 一个差点被IoGetDeviceObjectPointer搞死的菜鸟
- Java Object类方法
- iOS开发Objective-C编码规范
- Java 类型转换以及Object转成其他类型
- Objective-C第2版第9章 内存管理
- Objective-C第2版第6章源文件组织学习记录
- 警告:Could not resolve external type c:objc(cs)NSObject