2012-11-15 72 views
0

我的代碼有這個錯誤。我正在設計一個自定義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; 

爲什麼我得到這個錯誤?任何幫助都會很棒。如果您需要更多信息,請詢問。謝謝...

+0

你是否正在使用'btn_CheckBox'在其他地方的其他類? – Robin

+1

你檢查框架,導入和目標添加? –

+0

@Robin。我編輯了代碼。這就是我的代碼中使用btn_Checkbox的所有地方。 – RookieAppler

回答

0

您需要#import <UIKit/UIKit.h>位於.h文件的頂部。

+0

@ sbwoodside.I在我的頭文件 – RookieAppler

0

你能有一個重新審視一下你的錯誤,爲什麼它說

終止應用程序由於未捕獲的異常「NSInvalidArgumentException」的理由是:「+的UIButton buttonWithType:]:無法識別的選擇發送到0x3a88班」 。這個??

理由:「+ [* UIBU * tton buttonWithType:]

它說UIBU,所以我相信這是在UIButton的拼寫錯誤。

希望它有幫助。

+0

中已經有import語句,在這裏我把它當作UIBUtton..on在它的實際控制檯中輸入它說UIButton buttonWithType:.. .. etc.itsits在這裏打字錯誤,但不是在實際的輸出。 – RookieAppler

0

在行:

[btn_CheckBox addTarget:self action:@selector(handleTap) forControlEvents:UIControlEventTouchUpInside]; 

是方法handleTap狀態爲- (void)handleTap { }- (void)handleTap:(id)sender?如果是後者,你的行應該更新爲:

[btn_CheckBox addTarget:self action:@selector(handleTap:) forControlEvents:UIControlEventTouchUpInside];