rtm
rtm

Reputation: 81

iOS Objective-C Controlling Button Images

I have a button. Unsure if it is specifically a UIButton class type. This is a Button which was dragged from the Object library into my Main.Storyboard.

I don't have text, instead I have an image. And I plan to use two images. One for "loop on" and the other for "loop off". I'm playing a sound and this button controls whether or not you will loop the sound. I'm all set with understanding how to do the looping versus not.

I have set the "loop off" image as my default image under the Attributes inspector.

So when the button gets touched, I'll change the behavior of my playing sound to be "loop forever" and I'd like to change the image of the button from within my IBAction function.

I can set a title for the button in the Attributes inspector.

But I don't see that title as a resource name (auto-typing-complete) from within my ViewController.m code.

I know there are properties per state for images. I believe there's also just a "setImage" action for a UIButton.

What I can't determine is how my Button is named in the code because this button was added via dragging it out of the Object library.

Anyone have any knowledge of where these resources get named, like a reference file?

A search found something close, the following link:

http://ranga-iphone-developer.blogspot.com/2011/08/how-to-change-uibutton-image.html

However the solution doesn't appear to reference a particular button name, title, or ID; unless the text "button" is supposed to be replaced with the specific resource name.

Upvotes: 0

Views: 173

Answers (3)

Nitheesh George
Nitheesh George

Reputation: 1367

Have you added an IBOutlet for this UIButton in your code. If not see the link

Upvotes: 1

Boris
Boris

Reputation: 11693

Create a property for the button in the view controller header

@property (nonatomic, strong) IBOutlet UIButton *yourButton;

Then link the outlet in the Interface Builder (i.e. control drag from the controller to the button) and select yourButton, now you can change the title, color and images of the button by using yourButton.

Upvotes: 1

Prasad
Prasad

Reputation: 6034

You have to create outlets inside the code for your elements added in storyboard. After that you can programatically modify the properties of the elements.

Take a look at this for IBOutlets Link on Outlets

Upvotes: 1

Related Questions