How do you change the text of the button and disable a button in iOS?
You can omit UIControlState part and just write like button. setTitle("my text here", forState: . Normal) .
To disable a button, you should use the disabled view modifier. It takes bool parameter, which for example, can be taken from the state. You can use the disabled modifier on a view above buttons ( VStack , for example). That way, you will apply it to all buttons (or other controls below).
The quickest way to disable the button in Swift is to simply set the button's isEnabled property to false. For example button. isEnabled = false.
Hey Namratha, If you're asking about changing the text and enabled/disabled state of a UIButton, it can be done pretty easily as follows;
[myButton setTitle:@"Normal State Title" forState:UIControlStateNormal]; // To set the title [myButton setEnabled:NO]; // To toggle enabled / disabled
If you have created the buttons in the Interface Builder and want to access them in code, you can take advantage of the fact that they are passed in as an argument to the IBAction
calls:
- (IBAction) triggerActionWithSender: (id) sender;
This can be bound to the button and you’ll get the button in the sender
argument when the action is triggered. If that’s not enough (because you need to access the buttons somewhere else than in the actions), declare an outlet for the button:
@property(retain) IBOutlet UIButton *someButton;
Then it’s possible to bind the button in IB to the controller, the NIB loading code will set the property value when loading the interface.
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