button點擊切換背景圖片的問題

我做了個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];

    }
這樣就可以了,具體還沒知道原因!!,有知道的請多多指點哈

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章