2012-12-18 42 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

這是很簡單,但我得到了當我更改代碼中的插座類型時,發現完全相同的錯誤 - 看起來它仍然連接在故事板中,但事實並非如此。我只需將故事板中的插座(例如標籤)重新連接到代碼插座。你不會這麼想,因爲這個插座仍然被標記爲連接,但是如果你已經做出了改變,那不是這樣。

相關問題