Snorlax Snorlax - 6 months ago 28
Swift Question

Swift, Xcode: Changing UIButton Background and Text on Click

I want to change the background and text of the button on click, I tried several SO solutions but they haven't worked, you can see what I tried in my project:
https://github.com/jzhang172/modalTest

I tried debugging it by putting a simple print statement and it looks like it doesn't ever go to it.

Answer

SOLUTION:

1) Create an IBAction from your UIButton and also an IBOutlet called button.

EDIT: As per your request (How to trigger the even when the button is TOUCHED, not RELEASED?):

enter image description here

2) Do this:

@IBAction func changes (sender: UIButton) {

    button.backgroundColor = UIColor.blueColor()

    button.setTitle("Button Title", forState: UIControlState.Normal)

}