I have UIButton instance called card. I have an object called “Representer”, which has a property “image”, which is a UIImage. I’m trying to change the value of the button’s image at runtime. I have the button in interface builder, and also declare an IBOutlet with the button. I write this code :
UIButton *card1 = [[UIButton alloc] init];
[card1 setImage:representer.image forState:UIControlStateNormal];
When this code is compiled, the button’s image doesn’t change. Someone help me, please!
You first need to ensure that your button is connected to the IBOutlet in Interface Builder, have you done that with the Connections Inspector?
If you have the button in your .h as:
Then, in the .m file, call
In your code, you are just instantiated another UIButton and setting the image on it. That is fine code but has nothing to do with the button in the xib.
This definitely assumes the IBOutlet is correctly wired up in Interface Builder. Here’s how it should look:
Also, you need to be consistent in the button’s image, versus the button’s background image. They are two different things. If you have specified the “image” in Interface Builder, then do the same in your code, and vice versa with the backgroundImage.