View Full Version : Changing text on a button

Jul 23, 2008, 10:49 PM
Anyone knows how to change the text permanently on a rounded rect button upon clicked?

Jul 23, 2008, 11:00 PM
Would you just change the "title" property on a touch event?

btnFoo.title = @"new text";

Correct me if I'm wrong?

You can learn how to capture events from buttons by just looking at some of the sample Projects on the SDK site.


Jul 24, 2008, 12:26 AM
[myButton setTitle:@"Normal title" forState:UIControlStateNormal];
[myButton setTitle:@"Pressed title" forState:UIControlStateHighlighted];

Jul 24, 2008, 12:44 AM
Thanks Sbrocket for correcting me.

Jul 24, 2008, 01:42 AM
Firstly, thanks for helping!

And sadly, It doesn't work for me..

This is the code:

[button setTitle:@"Finish" forstate: UIControlStateHighlighted];

I put it in a function, which will trigger when button is clicked. It can run the program, but it doesn't change the text. Oh yeah, thats a warning to that line of code:

"UIButton" may not respond to "-setTitle:forstate"

And forgive me, I wasn't clear on my question, what I meant is when I clicked the button, the text will be changed permanently... Is that possible to do that? The codes in the header files doesn't look like doing this kind of thing...

Jul 24, 2008, 03:28 AM
Again, sorry. I actually forgot to write IBOutlet when declaring a button, thus cannot link. Now, it works like a charm!

Sorry and thanks for the help!:)

Jul 29, 2010, 05:35 AM
I just found this answer and used it

Thanks from the future.

Nov 9, 2010, 08:42 AM
I also found this in the future, but I'm going to have to bring the past back to life because it didn't work for me :(

I've made a simple stopwatch with a start/stop button, and what i'm trying to do is change the stop button to stay "reset" after it's clicked once, and revert back to "stop" when the stopwatch is running.

The solution in this thread is pretty much exactly how I'd written my code. I defined an IBOutlet UIButton in my header file. I hooked up the outlet in interface builder. And in a few places in the code I call setTitle and pass it UIControlStateNormal, because I just want it to change the normal non-highlighted/selected state.

It's not working and I really have no idea why. Something that did occur to me as I was writing this is that I did set the initial title for the button in interface builder. Could that have something to do with it?

Anyways, sorry to resurrect a dinosaur, but this seemed a good place to post my problem.

Nov 9, 2010, 07:08 PM
I figured out my problem and it was basically a total brain fart on my part. Everything here is working just fine.

Nov 9, 2010, 07:11 PM
I figured out my problem and it was basically a total brain fart on my part. Everything here is working just fine.
Care to clue us in as to what the issue was, so we can try to avoid our own brains farting in this way? ;)

Nov 9, 2010, 07:51 PM
Heh, I almost posted it but I was embarassed.

For some reason I had set the button = nil in my viewDidLoad method. I think I had done that and then had some configuration of the button properties after that at one point and I deleted the properties and just set the title and font and all that in interface builder, but apparently I didn't delete the "buttonName = nil" part.

So yea, message to all you future searchers...don't set your buttons equal to nil :(