我的代碼有這個錯誤。我正在設計一個自定義checkbox
。爲此,我有一個UIView
,我已將其定製爲看起來像Checkbox
。我有我的項目的另一個副本,我使用的代碼,它工作得很好。我將代碼複製到另一個mac,它不會工作。它是相同的代碼。UIButton UIButtonTypeCustom error.Unrecognized selector sent to class
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;
爲什麼我得到這個錯誤?任何幫助都會很棒。如果您需要更多信息,請詢問。謝謝...
你是否正在使用'btn_CheckBox'在其他地方的其他類? – Robin
你檢查框架,導入和目標添加? –
@Robin。我編輯了代碼。這就是我的代碼中使用btn_Checkbox的所有地方。 – RookieAppler