簡體   English   中英

更改UIImageView的image屬性的問題

[英]Problems with changing the image property of a UIImageView

我有一個UIImageView,當一個調用的函數想要更改為另一個(“活動”)圖像時,另一個調用的更改為之前的圖像。 這是代碼:

- (NavButton *)initWithFrame:(CGRect *)fr andImage:(UIImage *)img andActiveImage:(UIImage *)acImg {
    NavButton *a = [[NavButton alloc] initWithFrame:*fr];
    [a setBackgroundColor:[UIColor clearColor]];
    UIImageView *aImg = [[UIImageView alloc] initWithFrame:CGRectMake(8.5, 8.5, 28, 28)];
    aImg.tag = 13;
    aImg.image = img;
    self.orginalImage = img;
    self.activeImage = acImg;
    [a addSubview:aImg];
    return a;
}

- (void)setIsActive:(NSNumber *)isActive {
    self.active = isActive;
    if ([isActive isEqualToValue:[NSNumber numberWithBool:NO]]) {
        [self undoActive];
    } else {
        [self redoActive];
    }
}

- (void)undoActive {
    UIImageView *a = (UIImageView *)[self viewWithTag:13];
    a.image = self.orginalImage;
}

- (void)redoActive {
    UIImageView *a = (UIImageView *)[self viewWithTag:13];
    a.image = self.activeImage;

}

當我調用[btn setIsActive:[NSNumber numberWithBool:YES]]; [btn setIsActive:[NSNumber numberWithBool:NO]]; 兩次都刪除圖像,但是當我不打電話時,圖像都停留在那里。 那么,當我打電話給他們時,如何將按鈕的圖像更改為正確的圖像呢?

您可以將兩個圖像分配給該圖像視圖,而不是將圖像重復分配給imageview:一個分配給“ image”屬性,另一個分配給“ highlightedImage”屬性。 要在圖像之間切換時,請將布爾屬性“突出顯示”設置為“是”或“否”。

只需按以下方式進行檢查就會更容易:

if (![isActive boolValue]) {

然后,進行一些調試,添加一些斷點和/或記錄。 檢查實際接收到哪些值。 標志設置是否正確。 圖像設置是否正確。 什么都nil

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM