Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change text of button and disable button in iOS

Tags:

text

button

ios

How do you change the text of the button and disable a button in iOS?

like image 307
Namratha Avatar asked Feb 10 '11 07:02

Namratha


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 disable a button in Xcode?

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).

How do you disable a button in Swift?

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.


1 Answers

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.

like image 118
MGunetileke Avatar answered Sep 27 '22 20:09

MGunetileke