Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

changing the value in a text box

I intend to change the value contained in a text box by pressing a Button.(My app takes some value from text boxes previously input by the user and then change the values according to the " units chosen" by the User. the Units command is triggered by clicking on a action button which i also would like to switch the name on it from saying imperial to metric or visceversa).. So once you converted the value the new value is stored in the same input text box. Could anyone possibly help?

1 Answer

By text box do you mean UILabel or UITextField? If so, assuming your label or textfield has been assigned as the property self.textbox, to change the text, you would access its text property, i.e.

- (IBAction)buttonClicked{
  self.textbox.text = @"some text";
}

For a button, i.e. UIButton, the text is set with the setTitle:forState: method. As described in the documentation:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instm/UIButton/setTitle:forState: