先了解一下 objc_setAssociatedObject objc_getAssociatedObject
需要头文件 #import <objc/runtime.h>
Sets an associated value for a given object using a given key and association policy.
Declaration
void objc_setAssociatedObject(id object, void *key, id value, objc_AssociationPolicy policy)
Parameters
object | The source object for the association. |
key | The key for the association. |
value | The value to associate with the key key for object. Pass nil to clear an existing association. |
policy | The policy for the association. For possible values, see Associative Object Behaviors. |
功能:进行对象之间的关联,需要时,可以获取到关联的对象
四个参数:需要四个参数:源对象,关键字,关联的对象和一个关联策略。
//1 object:源对象
//2 key:进行关联的关键字 唯一静态变量key associatedkey
//3 value:关联的对象
//4 policy:关键策略 (
typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) {
OBJC_ASSOCIATION_ASSIGN = 0, /**< Specifies a weak reference to the associated object. */
OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< Specifies a strong reference to the associated object.
* The association is not made atomically. */
OBJC_ASSOCIATION_COPY_NONATOMIC = 3, /**< Specifies that the associated object is copied.
* The association is not made atomically. */
OBJC_ASSOCIATION_RETAIN = 01401, /**< Specifies a strong reference to the associated object.
* The association is made atomically. */
OBJC_ASSOCIATION_COPY = 01403 /**< Specifies that the associated object is copied.
* The association is made atomically. */
};
)
Returns the value associated with a given object for a given key.
Declaration
id objc_getAssociatedObject(id object, void *key)
Parameters
object | The source object for the association. |
key | The key for the association. |
功能:获取关联的对象
两个参数: object 源对象
key 进行关联的关键字
//举个栗子================================================
//UIButton+test.h———
#import <UIKit/UIKit.h>
@interface UIButton (test)
@property(nonatomic,assign)NSString *name;
@end
//UIButton+test.m——-
#import "UIButton+test.h"
#import <objc/runtime.h>
@implementation UIButton (test)
static const char Name;
-(void)setName:(NSString *)name
{
// 进行绑定,把name 和 btn 绑定起来,将来就可以通过 btn 获得 name 了
objc_setAssociatedObject(self, &Name, name, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
-(NSString *)name
{
// 通过self 和 关键字 获得绑定的name
return objc_getAssociatedObject(self, &Name);
}
@end
//TestViewController.h———
#import <UIKit/UIKit.h>
@interface TestViewController : UIViewController
@end
//TestViewController.m———
#import "TestViewController.h"
#import "UIButton+test.h"
@interface TestViewController ()
@end
@implementation TestViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *but = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
but.backgroundColor =[UIColor redColor];
but.name = @"Brian";
[but addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:but];
}
-(void)butClick:(UIButton *)but
{
NSLog(@"name= %@",but.name);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end