开发者

Temporarily change a UIButtons image

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜