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

David Contreras
David Contreras
6,187 Points

Changing the text of a button in swift

I am trying to change the text of a button. Can someone tell me if I'm close to achive it.

var buttn = funcName.titleLabel?.text
Tyler Simko
Tyler Simko
5,610 Points

Are you just trying to change the title of the button, or do you want to change it dynamically (like as the result of a button press)?

If it's the former, you can just double click on the button in your storyboard and type in a new title.

David Contreras
David Contreras
6,187 Points

dynamically is the way i want it to change

1 Answer

Tyler Simko
Tyler Simko
5,610 Points

Hey David,

Here's one way you could do it:

I put a button on my Storyboard, connected it as an IBOutlet called myButton and an IBAction called myButtonPressed, then put this code in the IBAction:

myButton.setTitle("Pressed", forState: UIControlState.Normal)

I selected my ControlState as normal, which would make "Pressed" the new default Button text, but there are other options as well described in the Documentation.