子类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对象必须做什么? 是独特的解决scheme?

谢谢!

尝试使用具有关联引用的类别来代替。 这是更清洁,将工作在UIButton所有实例。

的UIButton + Property.h

 #import <Foundation/Foundation.h> @interface UIButton(Property) @property (nonatomic, retain) NSObject *property; @end 

的UIButton + Property.m

 #import "UIButton+Property.h" #import <objc/runtime.h> @implementation UIButton(Property) static char UIB_PROPERTY_KEY; @dynamic property; -(void)setProperty:(NSObject *)property { objc_setAssociatedObject(self, &UIB_PROPERTY_KEY, property, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } -(NSObject*)property { return (NSObject*)objc_getAssociatedObject(self, &UIB_PROPERTY_KEY); } @end 

//示例用法

 #import "UIButton+Property.h" ... UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button1.property = @"HELLO"; NSLog(@"Property %@", button1.property); button1.property = nil; NSLog(@"Property %@", button1.property); 

你需要做的是:

 MyButton *btn = [[MyButton alloc] init]; 

创build你的button。 buttonWithType:UIButtonTypeRoundedRect只创buildUIButton对象。

===编辑===

如果你想使用RoundedRectbutton; 那么我会build议如下。 基本上,我们只是创build一个UIView与我们想要的任何属性,并添加到该视图所需的button。


。H

 @interface MyButton : UIView { int property; } @property int property; @end 

.M

 @implementation MyButton @synthesize property; - (id)initWithFrame:(CGRect)_frame { self = [super initWithFrame:_frame]; if (self) { UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = self.bounds; [self addSubview:btn]; } return self; } @end 

用法:

 MyButton *btn = [[MyButton alloc] initWithFrame:CGRectMake(0, 0, 200, 20)]; btn.property = 42; [self.view addSubview:btn]; 

我有一个简单的scheme,只涉及一些库方法,没有样板,只需要为每个属性添加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