**
**
有时需要在对象中存放相关信息。这时我们通常会从对象所属的类中继承一个子类,然后改用这个子类对象。然而并非所有情况下都能这么做,有时候类的实例可能是由某种机制所创建的,而开发者无法令这种机制创建出自己所写的子类实例。Objective-C中有一项强大的特性可以解决此问题,这就是“关联对象”(Associated Object)。
下列方法可以管理关联对象:
void objc_setAssociatedObject(id object, void*key, id value, objc_AssociationPolicy policy)
此方法以给定的键和策略为某对象设置关联对象值。
id objc_getAssociatedObject(id object, void*key)
此方法根据给定的键从某对象中获取相应的关联对象值。
void objc_removeAssociatedObjects(id object)
此方法移除指定对象的全部关联对象。
我们使用UIAlertView类,通过分类将简写,通过block关联到alertView.
如果想在同一个类里处理多个警告信息视图,那么代码就会变得更为复杂,我们必须在delegate方法中检查传入的alertView参数,并据此选用相应的逻辑。要是能在创建警告视图的时候直接把处理每个按钮的逻辑都写好,那就简单多了。这可以通过关联对象来做。创建完警告视图之后,设定一个与之关联的“块”(block),等到执行delegate方法时再将其读出来
#import <UIKit/UIKit.h>
typedef void (^SuccessBlock)(NSInteger index);
@interface UIAlertView (Block)<UIAlertViewDelegate>
- (void)showWithBlock:(SuccessBlock) successBlock;
@end
#import "UIAlertView+Block.h"
#import <objc/runtime.h>
static const char alertKey;
@implementation UIAlertView (Block)
- (void)showWithBlock:(SuccessBlock)successBlock{
if (successBlock) {
objc_setAssociatedObject(self, &alertKey, successBlock, OBJC_ASSOCIATION_COPY);
self.delegate = self;
}
[self show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
SuccessBlock block = objc_getAssociatedObject(self, &alertKey);
block(buttonIndex);
}
@end
还可以使用UIButton来将其与block关联
#import <UIKit/UIKit.h>
typedef void(^ButtonBlock)(UIButton *button);
@interface UIButton (btnBlock)
- (void)hanClickButtonWithBlock:(ButtonBlock)block;
@end
#import "UIButton+btnBlock.h"
#import <objc/runtime.h>
static const char buttonKey;
@implementation UIButton (btnBlock)
- (void)hanClickButtonWithBlock:(ButtonBlock)block{
if (block) {
objc_setAssociatedObject(self, &buttonKey, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
[self addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
}
}
- (void)clickButton{
ButtonBlock block = objc_getAssociatedObject(self, &buttonKey);
block(self);
}
@end
在viewController中使用
#import "ViewController.h"
#import "UIAlertView+Block.h"
#import "UIButton+btnBlock.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 30, 30)];
[button setTitle:@"buttonBlock" forState:UIControlStateNormal];
button.backgroundColor = [UIColor greenColor];
[self.view addSubview:button];
[button hanClickButtonWithBlock:^(UIButton *button) {
NSLog(@"%@",button.titleLabel.text);
[self click];
}];
}
- (void)click{
UIAlertView *alerts = [[UIAlertView alloc] initWithTitle:@"www" message:@"111" delegate:self cancelButtonTitle:@"q" otherButtonTitles:@"yyy", nil];
[alerts showWithBlock:^(NSInteger index) {
if (index == 0) {
NSLog(@"1");
}
if (index == 1) {
NSLog(@"2");
}
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
以这种方式改写之后,创建警告视图与处理操作结果的代码都放在一起了,这样比原来更易读懂,因为我们无须在两部分代码之间来回游走,即可明白警告视图的用处。但是,采用该方案时需注意:块可能要捕获(capture)某些变量,这也许会造成“循环引用”(retain cycle)。
正如大家所见,这种做法很有用,但是只应该在其他办法行不通时才去考虑用它。若是滥用,则很快就会令代码失控,使其难于调试。“循环引用”产生的原因很难查明,因为关联对象之间的关系并没有正式的定义(formal definition),其内存管理语义是在关联的时候才定义的,而不是在接口中预先定好的。使用这种写法时要小心,不能仅仅因为某处可以用该写法就一定要用它。想创建这种UIButton还有个办法,那就是从中继承子类,把块保存为子类中的属性。