Changing button text multiple times

Hi - I'm trying to get the text on a button to change to something different more than once. So for example, the button initially says "blue", then when the user taps it the text changes to "red", then when the user taps it again the text changes to "yellow".


I can get the text to change once (from "Blue" to "Red") if I set the initial Button text to "Blue" in the Main storyboard by double-clicking on the button, and then using the below code to change the Button text to "Red" when it's tapped. But I can't work out how to change the text more than once. Any help much appreciated!


@IBAction func buttonTapped(sender: UIButton) {

sender.setTitle("Red", forState: UIControlState.Normal)

}

Answered by jeastwood in 176815022

Hello Johanna,


I am just learning Swift myself however I this code seems to do what you are asking:

var buttonCounter = 1 // Set this as a global variable in ViewController.swift

  @IBAction func buttonAction(_ sender: AnyObject) { //Create your button action

  buttonCounter += 1 // Increment buttonCounter variable by 1

  switch buttonCounter { // Perform a switch statement to get value of buttonCounter and run code in matching case

  case 1:
  sender.setTitle("Red", for: .normal) // Also the initial title for the button in Xcode

  case 2:
  sender.setTitle("Green", for: .normal)

  case 3:
  sender.setTitle("Blue", for: .normal)
  buttonCounter = 0

  default:
  print("Unable to change button title text.")

  }


If your button is going to start with one of the named colours as the title, for example 'Red" - declare your buttonCounter variable to start with a value of 1 and set the title in Xcode (Interface Builder) as 'Red'. On each button press, buttonCounter will go up by 1 and the next switch statement will be processed until 'case 3' sets buttonCounter to 0 - the next button press increases that value by 1 so button title becomes 'Red' again.


Hope that this helps.


James

Accepted Answer

Hello Johanna,


I am just learning Swift myself however I this code seems to do what you are asking:

var buttonCounter = 1 // Set this as a global variable in ViewController.swift

  @IBAction func buttonAction(_ sender: AnyObject) { //Create your button action

  buttonCounter += 1 // Increment buttonCounter variable by 1

  switch buttonCounter { // Perform a switch statement to get value of buttonCounter and run code in matching case

  case 1:
  sender.setTitle("Red", for: .normal) // Also the initial title for the button in Xcode

  case 2:
  sender.setTitle("Green", for: .normal)

  case 3:
  sender.setTitle("Blue", for: .normal)
  buttonCounter = 0

  default:
  print("Unable to change button title text.")

  }


If your button is going to start with one of the named colours as the title, for example 'Red" - declare your buttonCounter variable to start with a value of 1 and set the title in Xcode (Interface Builder) as 'Red'. On each button press, buttonCounter will go up by 1 and the next switch statement will be processed until 'case 3' sets buttonCounter to 0 - the next button press increases that value by 1 so button title becomes 'Red' again.


Hope that this helps.


James

You need to maintain state somewhere to keep track of what the current color is. Then "increment" that value each time the button is pressed and set the title based upon that value. There are numerous ways to do this.

Thanks very much James, this was really helpful!

Changing button text multiple times
 
 
Q