I have assigned a button (holdCardOne) an image in the interface builder. What I want to do is change the image when a card is selected from a picker. I have the picker working and selecting the card but I need to change the image on the original selected button to resemb开发者_运维知识库le the selection.
Can I do this using code?
Does it matter I assigned the image in the interface builder or does it have to be all done by code?
You should be able to change the button image using the setImage:forState:
method of the UIButton class. See the UIButton class reference for more info.
In terms of using the Interface Builder, it would be safe to set the initial image in that, but you'll need to handle things yourself from that point onwards. (e.g.: If a user can un-associate an image with a button, you'll need to set an appropriate image programatically as above.)
You can do it either in code or in the interface builder or both!
InterfaceBuilder lets you assign images for each button state (normal, highlighted, selected) -- so it could be as easy as assigning images for each state, and then simply changing the state in your code.
Alternatively, you can set the image directly, simply by saying:
[myButton setImage: (someImage) forState: UIControlStateNormal];
You might want to also set this image for the other buttons states (highlighted, selected), it all depends on how you want the interface to behave.
It doesn't matter that it was assigned in Interface Builder. All you need to do is this:
[yourButtonName setImage:yourNewImageName forState:UIControlStateNormal];
You'll also likely want to set an image for
UIControlStateHighlighted
and possibly for
UIControlStateDisabled
and
UIControlStateSelected
精彩评论