为了账号安全,请及时绑定邮箱和手机立即绑定

子类UIButton添加属性

子类UIButton添加属性

慕容森 2019-08-02 14:33:09
子类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唯一的解决方案?谢谢!
查看完整描述

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


查看完整回答
反对 回复 2019-08-02
  • 3 回答
  • 0 关注
  • 633 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信