3 回答
TA贡献1873条经验 获得超9个赞
我有一个简单的方案,只涉及一些库方法,没有样板,只有3行代码,你想要添加每个属性。下面添加了两个示例属性:startPoint和tileState。为了便于说明,这里是您需要为像tileState这样的属性添加的行:
//@property (assign, nonatomic) SCZTileState tileState; // tileState line 1
//@property (assign, nonatomic) SCZTileState tileState; // tileState line 2
//@dynamic tileState; // tileState line 3
我的博客文章中有更多细节描述了它的工作原理
的UIButton + SCZButton.h
#import <UIKit/UIKit.h>
@interface UIButton (SCZButton)
@property (readwrite, nonatomic) id assocData;
@end
的UIButton + SCZButton.m
// UIButton+SCZButton.m
// Copyright (c) 2013 Ooghamist LLC. All rights reserved.
#import "UIButton+SCZButton.h"
#import <objc/runtime.h>
@implementation UIButton (SCZButton)
- (id)assocData {
id data = objc_getAssociatedObject(self, "SCZButtonData");
return data;
}
- (void)setAssocData:(id)data {
objc_setAssociatedObject(self, "SCZButtonData", data,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
OOGTotallyTile.h
// UIButton+OOGTotallyTile.m
// Copyright (c) 2013 Ooghamist LLC. All rights reserved.
#import <UIKit/UIKit.h>
#import "UIButton+SCZButton.h"
#define kPointLabelTag 837459
typedef enum {
SCZTileStatePlaced,
SCZTileStateDropping,
SCZTileStateDropped
} SCZTileState;
@interface SCZButtonData : NSObject
@property (assign, nonatomic) CGPoint startPoint;
@property (assign, nonatomic) SCZTileState tileState; // tileState line 1
@end
@interface UIButton (OOGTotallyTile)
@property (readonly, nonatomic) SCZButtonData *buttonData;
@property (assign, nonatomic) CGPoint startPoint;
@property (assign, nonatomic) SCZTileState tileState; // tileState line 2
@end
OOGTotallyTile.m
// UIButton+OOGTotallyTile.m
// Copyright (c) 2013 Ooghamist LLC. All rights reserved.
#import "OOGTotallyTile.h"
@implementation SCZButtonData
@end
@implementation UIButton (OOGTotallyTile)
@dynamic startPoint;
@dynamic tileState; // tileState line 3
- (SCZButtonData*)buttonData {
if ( ! self.assocData) {
self.assocData = [[SCZButtonData alloc] init];
}
return self.assocData;
}
- (id)forwardingTargetForSelector:(SEL)aSelector {
id forwardingTarget = [super forwardingTargetForSelector:aSelector];
if ( ! forwardingTarget) {
return [self buttonData];
}
return forwardingTarget;
}
@end
- 3 回答
- 0 关注
- 633 浏览
添加回答
举报