UIButton UIButtonTypeCustom error.Unrecognized选择器发送到类

时间:2012-11-15 04:04:43

标签: objective-c ios checkbox uibutton invalid-argument

我的代码出错了。我正在设计自定义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;

为什么我会收到此错误?任何帮助都会很棒。如果您需要更多信息,请询问。感谢...

3 个答案:

答案 0 :(得分:0)

您的.h文件顶部需要#import <UIKit/UIKit.h>

答案 1 :(得分:0)

您可以重新查看一下您的错误,为什么会这样说

因未捕获的异常而终止应用程序'NSInvalidArgumentException'原因:'+ [UIBUtton buttonWithType:]:无法识别的选择器发送到类0x3a88'。这个??

原因:'+ [* UIBU * tton buttonWithType:]

它说UIBU,所以我相信UIButton有拼写错误。

希望它有所帮助。

答案 2 :(得分: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];
相关问题