【发布时间】:2010-09-07 22:06:36
【问题描述】:
我为 UIButton 创建了一个子类,并希望将自定义属性传递给它。但是,它不起作用,我从那以后读到子类化 UIButton 不是一个好主意。
我的问题是如何将自定义属性分配给按钮?我正在创建一个按钮,该按钮被放置在分组表的标题视图中。我想将部分编号传递给该按钮。我已经成功地在每一行中使用了一个 UISwitch,但不能对 UIButton 使用相同的方法。
所以我创建了一个按钮
MattsButtonClass *button = [MattsButtonClass buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(240, 15, 45, 30);
button.MFsectionNo = section; //this would be 1, 2 etc
//etc
那么我该怎么做呢?你会看到我的 UIbutton 有一个子类,看起来像这样
//.h file
@interface MattsButtonClass : UIButton {
int MFsectionNo;
}
@property (nonatomic) int MFsectionNo;
@end
//.m file
@implementation MattsButtonClass
@synthesize MFsectionNo;
@end
上面的错误是
-[UIRoundedRectButton MFsectionNo:]: unrecognized selector sent to instance 0x5673650
2010-09-07 22:41:39.751 demo4[67445:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIRoundedRectButton MFsectionNo:]: unrecognized selector sent to instance 0x5673650'
我正在尝试的事情是不可能的吗?感谢您的帮助...
【问题讨论】: