我做了個UIButton的擴展,代碼如下:
#import <UIKit/UIKit.h>
@interface CheckBoxButton : UIButton
@property (nonatomic,assign) BOOL isChecked;
@end
我通過下面代碼來實現點擊後切換圖片,但是並不能達到效果
if (button.isChecked) {
self.collectionButton.imageView.image = [UIImage imageNamed:@"shouchang_do"];
}else
{
self.collectionButton.imageView.image = [UIImage imageNamed:@"shouchang_undo"];
}
這樣設置後,點擊按鈕,圖片並沒有變換,解決辦法:
if (button.isChecked) {
//self.collectionButton.imageView.image = [UIImage imageNamed:@"shouchang_do"];
[self.collectionButton setImage:[UIImage imageNamed:@"shouchang_do"] forState:UIControlStateNormal];
}else
{
//self.collectionButton.imageView.image = [UIImage imageNamed:@"shouchang_undo"];
[self.collectionButton setImage:[UIImage imageNamed:@"shouchang_undo"] forState:UIControlStateNormal];
}
這樣就可以了,具體還沒知道原因!!,有知道的請多多指點哈