子类UIButton添加属性

子类UIButton添加属性

我想子类UIButton添加一些我需要的属性(不是方法...只有属性)。

这是我的子类的代码:

//.h-----------------------@interface MyButton : UIButton{
    MyPropertyType *property;}@property (nonatomic,retain) MyPropertyType *property;@end//.m--------------------------@implementation MyButton@synthesize property;@end

在这里我如何使用该类:

MyButton *btn = ((MytButton *)[MyButton buttonWithType:UIButtonTypeRoundedRect]);btn.property = SomeDataForTheProperty;

从哪里获得此错误:

 -[UIRoundedRectButton setProperty:]: unrecognized selector sent to instance 0x593e920

因此,从ButtonWithType我获得一个UIRoundedRectButton(Mybutton *)不能施展它...我需要做什么来获得一个MyButton对象?是-init唯一的解决方案?

谢谢!


慕容森
浏览 602回答 3
3回答

眼眸繁星

我有一个简单的方案,只涉及一些库方法,没有样板,只有3行代码,你想要添加每个属性。下面添加了两个示例属性:startPoint和tileState。为了便于说明,这里是您需要为像tileState这样的属性添加的行://@property (assign, nonatomic) SCZTileState tileState; // tileState line 1&nbsp;//@property (assign, nonatomic) SCZTileState tileState; // tileState line 2&nbsp;//@dynamic tileState;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// tileState line 3我的博客文章中有更多细节描述了它的工作原理的UIButton + SCZButton.h#import <UIKit/UIKit.h>@interface UIButton (SCZButton)@property (readwrite, nonatomic) id assocData;@end的UIButton + SCZButton.m//&nbsp; UIButton+SCZButton.m//&nbsp; Copyright (c) 2013 Ooghamist LLC. All rights reserved.#import "UIButton+SCZButton.h"#import <objc/runtime.h>@implementation UIButton (SCZButton)- (id)assocData {&nbsp; &nbsp; id data = objc_getAssociatedObject(self, "SCZButtonData");&nbsp; &nbsp; return data;}- (void)setAssocData:(id)data {&nbsp; &nbsp; objc_setAssociatedObject(self, "SCZButtonData", data,&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OBJC_ASSOCIATION_RETAIN_NONATOMIC);}@endOOGTotallyTile.h//&nbsp; UIButton+OOGTotallyTile.m//&nbsp; Copyright (c) 2013 Ooghamist LLC. All rights reserved.#import <UIKit/UIKit.h>#import "UIButton+SCZButton.h"#define kPointLabelTag 837459typedef enum {&nbsp; &nbsp; SCZTileStatePlaced,&nbsp; &nbsp; SCZTileStateDropping,&nbsp; &nbsp; SCZTileStateDropped} SCZTileState;@interface SCZButtonData : NSObject@property (assign, nonatomic) CGPoint startPoint;@property (assign, nonatomic) SCZTileState tileState;&nbsp; &nbsp;// tileState line 1@end@interface UIButton (OOGTotallyTile)@property (readonly, nonatomic) SCZButtonData *buttonData;@property (assign, nonatomic) CGPoint startPoint;@property (assign, nonatomic) SCZTileState tileState;&nbsp; // tileState line 2@endOOGTotallyTile.m//&nbsp; UIButton+OOGTotallyTile.m//&nbsp; 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 {&nbsp; &nbsp; if ( ! self.assocData) {&nbsp; &nbsp; &nbsp; &nbsp; self.assocData = [[SCZButtonData alloc] init];&nbsp; &nbsp; }&nbsp; &nbsp; return self.assocData;}- (id)forwardingTargetForSelector:(SEL)aSelector {&nbsp; &nbsp; id forwardingTarget = [super forwardingTargetForSelector:aSelector];&nbsp; &nbsp; if ( ! forwardingTarget) {&nbsp; &nbsp; &nbsp; &nbsp; return [self buttonData];&nbsp; &nbsp; }&nbsp; &nbsp; return forwardingTarget;}@end
打开App,查看更多内容
随时随地看视频慕课网APP