1

我正在尝试创建此检查按钮,然后在我的项目中任何我想要的地方调用该检查按钮,并通过添加应有的图像使其工作,但我什至无法将其发送到 NSLog ......进行测试目的...

下面是按钮 .h 和 .m 文件。以及我正在调用按钮并尝试使用的视图......

CheckButton.h #导入

@interface CheckButton : UIButton {
    BOOL _checked;
}

@property (nonatomic, setter=setChecked:) BOOL checked;

-(void) setChecked:(BOOL)check;

@end

检查按钮.m

#import "CheckButton.h"

@implementation CheckButton
@synthesize checked = _checked;

-(id) init {
    if (self=[super init]) {
        self.checked = NO;
        [self addTarget:self action:@selector(setChecked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

-(void) awakeFromNib {
    self.checked = NO;
    [self addTarget:self action:@selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];

}

- (void) setChecked:(BOOL)check{
    _checked = check;
    if(_checked) {
        UIImage *img = [UIImage imageNamed:@"check.png"];
        [self setImage:img forState:UIControlStateNormal];
         NSLog(@"Checked");
    } else {
        UIImage *img = [UIImage imageNamed:@"uncheck.png"];
        [self setImage:img forState:UIControlStateNormal];
         NSLog(@"UnChecked");
    }
}

-(void) OnCheck:(id) sender {
    self.checked = _checked;
}


@end

GoToBedPopUp.h

#import "CheckButton.h"

@interface GoToBedPopup : PopupContainer{
    IBOutlet  CheckButton *checkboxButton;
}

// Checkboxes - (CheckButton)
@property (nonatomic, strong) CheckButton *checkHR;

@end

GoToBedPopUp.m

#import "GoToBedPopup.h"

@implementation GoToBedPopup


@synthesize checkHR, checkO2, checkMovement, checkNoise, checkSkinTemp;

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];

    // Checkbox Button
    checkHR = [[CheckButton alloc] initWithFrame:CGRectMake(17, 31, 23, 23)];

    return self;
}
4

1 回答 1

1

嗨,我测试了你的代码,一切都好,你只改变一件事....

- (void) setChecked:(BOOL)check{
    _checked = !check; //** this is the only one change needed 
    if(_checked) {
        UIImage *img = [UIImage imageNamed:@"Check.png"];
        [self setImage:img forState:UIControlStateNormal];
        NSLog(@"Checked");
    } else {
        UIImage *img = [UIImage imageNamed:@"Uncheck.png"];
        [self setImage:img forState:UIControlStateNormal];
        NSLog(@"UnChecked");
    }
}

示例项目在这里

于 2013-04-23T11:16:43.483 回答