0

我有一个 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]];两次调用时,它会删除图像,但是当我不调用任何一个时,图像都会保留在那里。那么,我该如何做到这一点,所以当我调用它们时,它会将按钮的图像更改为正确的图像?

4

2 回答 2

3

您可以将两个图像分配给图像视图,而不是重复地将图像分配给 imageview:一个分配给“image”属性,另一个分配给“highlightedImage”属性。当您想在图像之间切换时,将布尔属性“highlighted”设置为 YES 或 NO。

于 2013-09-22T19:16:11.813 回答
1

只需进行以下检查会更容易:

if (![isActive boolValue]) {

然后,进行一些调试,添加一些断点和/或日志记录。检查实际接收到的值。标志设置是否正确。图像设置是否正确。是什么nil

于 2013-09-22T18:02:52.107 回答