j___.___j
j___.___j

Reputation: 326

Swift 3: Add text to label if button is pressed

In my Xcode project, I have five empty text labels (i.e. I, II, III, IV, V) and five buttons, each representing one letter (i.e. a, b, c, d, e).

I'd like to fill the text labels I to V with the letters, but in the order the letter buttons are pressed (i.e. user presses c → label I contains letter c; then user presses a → label II contains letter a and so on).

Could you give me any hint / solution how to do this?

Thanks, J.

Edit: After Benjamins suggestion, I have now done the following:

import UIKit

class ViewController: UIViewController {


    // Letter Buttons
@IBOutlet weak var LetterOneButton: UIButton!
@IBOutlet weak var LetterTwoButton: UIButton!
@IBOutlet weak var LetterThreeButton: UIButton!
@IBOutlet weak var LetterFourButton: UIButton!
@IBOutlet weak var LetterFiveButton: UIButton!

// Word Fields
@IBOutlet weak var WordLetterOne: UILabel!
@IBOutlet weak var WordLetterTwo: UILabel!
@IBOutlet weak var WordLetterThree: UILabel!
@IBOutlet weak var WordLetterFour: UILabel!
@IBOutlet weak var WordLetterFive: UILabel!

// Counter
@IBOutlet weak var CounterLabel: UILabel!

// Skip Button
@IBOutlet weak var SkipButtonLabel: UIButton!

// Define Variables
var index: Int = 0
var labels: [UILabel] = [WordLetterOne, WordLetterTwo, WordLetterThree, WordLetterFour, WordLetterFive]
var letters: [String] = ["A", "B", "C", "D", "E"]



override func viewDidLoad() {
    super.viewDidLoad()

    // Set initial letters for LetterButtons
    LetterOneButton.setTitle("A", for: .normal)
    LetterTwoButton.setTitle("U", for: .normal)
    LetterThreeButton.setTitle("T", for: .normal)
    LetterFourButton.setTitle("O", for: .normal)
    LetterFiveButton.setTitle("S", for: .normal)

    // Add button content to WordField
    // Second Attempt

    //you need to set the tags of each button in order for my method to work
    LetterOneButton.tag = 0
    LetterTwoButton.tag = 1
    LetterThreeButton.tag = 2
    LetterFourButton.tag = 3
    LetterFiveButton.tag = 4

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()



}

// Add button content to WordField
// First Attempt

@IBAction func LetterOneButtonPressed(_ sender: AnyObject) {
    labels[index].text = letters[sender.tag]

    index += 1
}

/**
@IBAction func LetterTwoButtonPressed(_ sender: AnyObject) {
    WordLetterTwo.text = LetterTwoButton.currentTitle
}

@IBAction func LetterThreeButtonPressed(_ sender: AnyObject) {
    WordLetterThree.text = LetterThreeButton.currentTitle
}

@IBAction func LetterFourButtonPressed(_ sender: AnyObject) {
    WordLetterFour.text = LetterFourButton.currentTitle
}

@IBAction func LetterFiveButtonPressed(_ sender: AnyObject) {
    WordLetterFive.text = LetterFiveButton.currentTitle
} **/




// Skip Button Action
@IBAction func SkipButtonAction(_ sender: AnyObject) {
}

}

Upvotes: 3

Views: 4506

Answers (2)

Benjamin Lowry
Benjamin Lowry

Reputation: 3789

What I would suggest is placing your labels and letters in an array:

var labels: [UILabel] = [label1, label2, ...]
var letters: [String] = ["a", "b", ...]

Then you could create a class variable to keep track of how many buttons have been pressed:

var index: Int = 0

Then in a IBAction that all the buttons are linked to for "touchUpInside" you could take all of this information and populate one label each time it is called. This of course would require you to set your buttons' tags as 0, 1, 2, 3, 4 (for a through e respectively).

@IBAction func buttonPressed(sender: UIButton){

    switch sender.tag {
    case 0:
        labels[index].text = letters[sender.tag]
    ...
    }

    index += 1

}

EDIT:

After seeing your code, you should change it to something like this:

 override func viewDidLoad() {
    super.viewDidLoad()

    // Set initial letters for LetterButtons
    LetterOneButton.setTitle("A", for: .normal)
    LetterTwoButton.setTitle("U", for: .normal)
    LetterThreeButton.setTitle("T", for: .normal)
    LetterFourButton.setTitle("O", for: .normal)
    LetterFiveButton.setTitle("S", for: .normal)

    // Add button content to WordField
    // Second Attempt

    //initialize array with real values
    labels = [WordLetterOne, WordLetterTwo, WordLetterThree, WordLetterFour, WordLetterFive]

    //you need to set the tags of each button in order for my method to work
    LetterOneButton.tag = 0
    LetterTwoButton.tag = 1
    LetterThreeButton.tag = 2
    LetterFourButton.tag = 3
    LetterFiveButton.tag = 4

}

For the index variable and arrays, make sure you initialize it OUTSIDE any function. Initialize them as class variables next to your labels and buttons like so:

@IBOutlet weak var WordLetterOne: UILabel!
@IBOutlet weak var WordLetterTwo: UILabel!
@IBOutlet weak var WordLetterThree: UILabel!
@IBOutlet weak var WordLetterFour: UILabel!
@IBOutlet weak var WordLetterFive: UILabel!

var index: Int = 0

var labels: [UILabel] = [UILabel]() //empty intialization

var letters: [String] = ["A", "B", "C", "D", "E"]

Then you need to change your other method to something like this:

@IBAction func LetterOneButtonPressed(sender: UIButton) {
    //you can actually omit the switch statement in your case
    labels[index].text = letters[sender.tag]

    index += 1
}

Keep in mind that you need to link EVERY button to the above method. Do not have different methods for each button!

Upvotes: 1

Litle Dev
Litle Dev

Reputation: 493

Hope this will hep you.

override func viewDidLoad() {
    super.viewDidLoad()

    var x = 0, y = 0, buttonWidth = 50, buttonHeight = 50

    for i in 0...5 {
        let tempButton = UIButton()
        tempButton.frame = CGRect(x: (x+5), y: (y+5), width: (buttonWidth-10), height: (buttonHeight-10))
        tempButton.tag = i
        tempButton.addTarget(self, action: #selector(buttonTapped(withButton: tempButton)), for: UIControlEvents.touchUpInside)
        tempButton.backgroundColor = .red

        x += buttonWidth
        self.view.addSubview(tempButton)
    }
}
func buttonTapped(withButton button:UIButton){
        let label:UILabel = UILabel() //your label
        if(button.tag == 0){
            label.text = "I"
        }else if(button.tag == 1){
            label.text = "II"
        }else if(button.tag == 2){
            label.text = "III"
        }else if(button.tag == 3){
            label.text = "IV"
        }else if(button.tag == 4){
            label.text = "V"
        }

    }

Upvotes: 0

Related Questions