[UIView setText:]:无法识别的选择器发送到实例0x89625d0

时间:2012-12-18 09:51:00

标签: iphone ios uiview

我试图在我的视图中添加一个自定义的复选框...我在我的故事板上绘制了一个视图,并在第三方代码UICheckbox的帮助下访问它。

UICheckbox.h

#import <UIKit/UIKit.h>

@interface UICheckbox : UIControl

-(void)setChecked:(BOOL)boolValue;
-(void)setDisabled:(BOOL)boolValue;
-(void)setText:(NSString *)stringValue;

@property(nonatomic, assign)BOOL checked;
@property(nonatomic, assign)BOOL disabled;
@property(nonatomic, strong)NSString *text;

@end

UICheckbox.m

#import "UICheckbox.h"

@interface UICheckbox (){
    BOOL loaded;
} 

@property(nonatomic, strong)UILabel *textLabel;

@end



@implementation UICheckbox
@synthesize checked = _checked;
@synthesize disabled = _disabled;
@synthesize text = _text;
@synthesize textLabel = _textLabel;

- (void)awakeFromNib {
    self.backgroundColor = [UIColor clearColor];
}

-(void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed:[NSString    stringWithFormat:@"uicheckbox_%@checked.png", (self.checked) ? @"" : @"un"]];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

    if(self.disabled) {
        self.userInteractionEnabled = FALSE;
        self.alpha = 0.7f;
    } else {
        self.userInteractionEnabled = TRUE;
        self.alpha = 1.0f;
    }

    if(self.text) {
        if(!loaded) {
            _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width +  5, 0, 1024, 30)];
            _textLabel.backgroundColor = [UIColor clearColor];
            [self addSubview:_textLabel];

            loaded = TRUE;
        }

        _textLabel.text = self.text;
     }
}

-(BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
    [self setChecked:!self.checked];
    return TRUE;
}

-(void)setChecked:(BOOL)boolValue {
    _checked = boolValue;
    [self setNeedsDisplay];
}

-(void)setDisabled:(BOOL)boolValue {
    _disabled = boolValue;
    [self setNeedsDisplay];
}

-(void)setText:(NSString *)stringValue {
    _text = stringValue;
    [self setNeedsDisplay];
}

@end

viewDidLoad ...

self.checkbox.text = @"Want to get the updates directly";

我实现了这样的动作

-(IBAction)testCheckbox:(id)sender {
    NSLog(@"checkbox.checked = %@", (self.checkbox.checked) ? @"YES" : @"NO");
}

从故事板的角度来看....我排队了插座并触摸到相应的消息并收到此错误..

请帮忙......

1 个答案:

答案 0 :(得分:1)

这很简单,但是当我更改代码中的插座类型时,我得到了完全相同的错误 - 看起来它仍然连接在故事板中,但事实并非如此。我只需将故事板中的插座(例如标签)重新连接到代码插座。您不会想到这一点,因为插座仍然标记为已连接,但如果您进行了更改则不会。