我試圖建立一個類我自定義複選框創建UI按鈕。出於某種原因,我的圖像沒有被Xcode設置。然而我在我的應用程序中有其他類正在設置圖像沒有問題。UI複選框按鈕不更改圖像
Checkbox.h
#import <UIKit/UIKit.h>
@interface checkBoxButton : UIButton
@property (nonatomic,assign) IBInspectable BOOL checked;
@end
Checkbox.m
#import "checkBoxButton.h"
@implementation checkBoxButton
-(id) init {
self = [super init];
if(self) {
[self addTarget:self action:@selector(changeState) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)checkedBox:(BOOL) checked{
self.checked = checked;
if(!self.checked){
[self setImage: [UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];
} else {
[self setImage: [UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
}
}
- (void) changeState {
self.checked = !self.checked;
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
@end
你確定'checkedBox:'方法正在觸發嗎? – Stonz2
誰在調用checkedBox? – GeneCode
[self sendActionsForControlEvents:UIControlEventValueChanged];正是我認爲會觸發它,但它沒有。 – Hakim