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