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?
You can omit UIControlState part and just write like button. setTitle("my text here", forState: . Normal) .
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.
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!") }
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; } }
UIButton *myButton; [myButton setTitle:@"My Title" forState:UIControlStateNormal]; [myButton setTitle:@"My Selected Title" forState:UIControlStateSelected];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With