我的代码有这个错误。我正在设计一个自定义checkbox
。为此,我有一个UIView
我定制的看起来像一个Checkbox
. 我有我的项目的另一个副本,我在其中使用了代码,它工作得非常好。我将代码复制到另一台mac,它不会工作。它是相同的代码。
My.m file
- (void)defaultInit
{
btn_CheckBox = [UIButton buttonWithType:UIButtonTypeCustom];
btn_CheckBox.frame = CGRectMake(0, 0, kImageWidth, kImageHeight);
btn_CheckBox.adjustsImageWhenHighlighted = NO;
[btn_CheckBox setImage:[UIImage imageNamed:@"CheckBox_Deselected.png"] forState:UIControlStateNormal];
[btn_CheckBox setImage:[UIImage imageNamed:@"CheckBox_Selected.png"] forState:UIControlStateSelected];
[btn_CheckBox addTarget:self action:@selector(handleTap) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn_CheckBox];
lbl_CheckBox = [[UILabel alloc] initWithFrame:CGRectMake(35, 0, self.bounds.size.width-35, 30)];
lbl_CheckBox.backgroundColor = [UIColor clearColor];
lbl_CheckBox.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
[self addSubview:lbl_CheckBox];
}
- (void)setID:(NSUInteger)index andSelected:(BOOL)state andTitle:(NSString *)title
{
nID = index;
[btn_CheckBox setSelected:state];
lbl_CheckBox.text = title;
}
- (void)handleTap
{
btn_CheckBox.selected = !btn_CheckBox.selected;
[delegate stateChangedForID:nID withCurrentState:btn_CheckBox.selected];
}
- (BOOL)currentState
{
return btn_CheckBox.selected;
}
我在第一行收到错误,method.btn_CheckBox = [UIButton buttonWithType:UIButtonTypeCustom];
一旦我的断点到达这一行,它就会抛出这个错误
Terminating app due to uncaught exception 'NSInvalidArgumentException' reason :'+[UIButton buttonWithType:]:unrecognized selector sent to class 0x3a88'.
我的 .h 文件
@protocol CheckBoxDelegate <NSObject>
- (void)stateChangedForID:(NSUInteger)index withCurrentState:(BOOL)currentState;
@end
@interface Checkbox : UIView
{
NSUInteger nID;
UIButton *btn_CheckBox;
UILabel *lbl_CheckBox;
id <CheckBoxDelegate> delegate;
}
@property (strong, nonatomic) UIButton *btn_CheckBox;
为什么我会收到此错误?任何帮助都会很棒。如果您需要更多信息,请询问。谢谢...