Change button text from Xcode?
113,794
Solution 1
If you've got a button that's hooked up to an action in your code, you can change the title without an instance variable.
For example, if the button is set to this action:
-(IBAction)startSomething:(id)sender;
You can simply do this in the method:
-(IBAction)startSomething:(id)sender {
[sender setTitle:@"Hello" forState:UIControlStateNormal];
}
Or if you're wanting to toggle the name of the button, you can create a BOOL
named "buttonToggled" (for example), and toggle the name this way:
-(IBAction)toggleButton:(id)sender {
if (!buttonToggled) {
[sender setTitle:@"Something" forState:UIControlStateNormal];
buttonToggled = YES;
}
else {
[sender setTitle:@"Different" forState:UIControlStateNormal];
buttonToggled = NO;
}
}
Solution 2
UIButton *myButton;
[myButton setTitle:@"My Title" forState:UIControlStateNormal];
[myButton setTitle:@"My Selected Title" forState:UIControlStateSelected];
Solution 3
Yes. There is a method on UIButton -setTitle:forState:
use that.
Solution 4
[myButton setTitle:@"Play" forState:UIControlStateNormal];
Solution 5
Another way to toggle:
- (IBAction)signOnClick:(id)sender
{
if ([_signOnButton.titleLabel.text isEqualToString:@"Sign off"])
{
[sender setTitle:@"Sign on" forState:UIControlStateNormal];
}
else
{
[sender setTitle:@"Sign off" forState:UIControlStateNormal];
}
}
Author by
Linuxmint
NSString *Linuxmint; Linuxmint = @"newToXcode"; NSLog (@"I have recently began serious iPhone Development"); NSLog (@"And I'm having lots of fun!");
Updated on July 20, 2022Comments
-
Linuxmint almost 2 years
I have a IBAction connected to a button in my Interface Builder.
Is it possible to change the text on the button (in IB) from within my code during runtime?