2012-12-18 47 views
0

我试图添加一个自定义的复选框,我的看法...我画我的故事板和第三方代码UICheckbox帮助我访问它。[UIView setText:]:无法识别的选择器发送到实例0x89625d0

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"); 
} 

从故事板视图....我排到了出口和感动到相应的消息,并得到这个错误..

请帮助...

+0

你是如何初始化你的复选框,你在哪里分配UICheckBox? –

+0

在我的.h文件中 @property(nonatomic,weak)IBOutlet UICheckbox *复选框; in .m文件 合成复选框的属性 viewdidLoad() self.checkbox.text = @“想要直接获取更新”; –

+0

尝试初始化该复选框,然后在设置复选框的文本之前将其作为子视图添加到viewDidLoad的self.view中。而且我认为你需要在你的UICheckBox自定义控件中重写initWithFrame:方法 –

回答

0

这是很简单,但我得到了当我更改代码中的插座类型时,发现完全相同的错误 - 看起来它仍然连接在故事板中,但事实并非如此。我只需将故事板中的插座(例如标签)重新连接到代码插座。你不会这么想,因为这个插座仍然被标记为连接,但是如果你已经做出了改变,那不是这样。

相关问题