Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change button text from Xcode?

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?

like image 217
Linuxmint Avatar asked Mar 10 '11 19:03

Linuxmint


People also ask

How do I change the text of a button in Swift?

You can omit UIControlState part and just write like button. setTitle("my text here", forState: . Normal) .

How do I change the label text on a button click in Swift?

Open your storyboard and view controller source side by side in the assistant editor. Then select the label. Switch to the connections inspector and then drag from the small circle next to "New Referencing Outlet" and into the view controller source file. This should create the @IBOutlet for you.

How do I make a text button in SwiftUI?

SwiftUI's button is similar to UIButton , except it's more flexible in terms of what content it shows and it uses a closure for its action rather than the old target/action system. To create a button with a string title you would start with code like this: Button("Button title") { print("Button tapped!") }


2 Answers

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;     } } 
like image 107
sudo rm -rf Avatar answered Sep 30 '22 21:09

sudo rm -rf


UIButton *myButton;  [myButton setTitle:@"My Title" forState:UIControlStateNormal]; [myButton setTitle:@"My Selected Title" forState:UIControlStateSelected]; 
like image 44
NWCoder Avatar answered Sep 30 '22 21:09

NWCoder