Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change button text in Swift Xcode 6?

Tags:

ios

swift

Here's what I'm trying to do. If you've ever played Halo or CoD, you'd know that you could change the name of a weapon load-out.

What I'm doing is making it so you can change your load-out name using a text field. Here's the problem, the load-out name in the load-out menu is a button (to select and view info about that load-out) and I could just write this:

@IBAction func renameClassButton(sender: AnyObject) {     classTopButton.text = "\(classTopTextField)" } 

Except it [classTopButton] is a button which doesn't allow the '.text' suffix

like image 978
GuavaMantis Avatar asked Oct 29 '14 22:10

GuavaMantis


People also ask

How do I change the button text on a click in Swift?

You can omit UIControlState part and just write like button. setTitle("my text here", forState: . Normal) .


2 Answers

You can do:

button.setTitle("my text here", forState: .normal) 

Swift 3 and 4:

button.setTitle("my text here", for: .normal) 
like image 109
Steve Rosenberg Avatar answered Sep 22 '22 12:09

Steve Rosenberg


In Xcode 8 - Swift 3:

button.setTitle( "entertext" , for: .normal )

like image 28
Nikola Lukic Avatar answered Sep 26 '22 12:09

Nikola Lukic