开发者

Temporarily change a UIButtons image

开发者 https://www.devze.com 2023-01-24 22:43 出处:网络
I have a UIButton which I assign an image in IB. I then change the image in the code, but I only want this to be temporary.

I have a UIButton which I assign an image in IB.

I then change the image in the code, but I only want this to be temporary.

How does one return the button to the image set in IB or is there a way开发者_开发问答 to revert back after doing the temporary switch?

Thanks


If you want to revert back to the previous image you would have to store a reference to it somewhere:

// prevImage is an ivar UIImage *
prevImage = [[myButton imageForState:UIControlStateNormal] retain];

// Change to the new image
...

// Later: revert back to prevImage
[myButton setImage:prevImage forState:UIControlStateNormal];
[prevImage release];
prevImage = nil;


UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"image1.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"image2.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:@"image3.png"] forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:@"image4.png"] forState:UIControlStateDisabled];

You can set your own custom images for different states. when you tap on it, the corresponding images will appear on your button.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号