Swift 3: 如果按下按钮,则向标签添加文本

3
在我的Xcode项目中,我有五个空文本标签(即I,II,III,IV,V)和五个按钮,每个按钮代表一个字母(即a,b,c,d,e)。
我想用字母填充标签I到V,但是按下字母按钮的顺序来填充(例如,用户按下c → 标签I包含字母c;然后用户按下a → 标签II包含字母a,以此类推)。
你能给我任何提示/解决方案如何做到这一点吗?
谢谢, J.
编辑:在Benjamin的建议之后,我现在已经执行了以下操作:
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) {
}

}

你如何判断整个按键会话已经结束?如果用户连续点击同一个按钮怎么办? - El Tomato
每个按钮在被触摸后都会被禁用,直到所有五个按钮都被触摸。 - j___.___j
请展示一下你目前的工作成果。 - Ozgur Vatansever
创建一个数组,并在用户点击任何按钮时将对应的字母放入其中。然后对数组进行排序。 - El Tomato
2个回答

1
我建议将标签和字母放在一个数组中:
var labels: [UILabel] = [label1, label2, ...]
var letters: [String] = ["a", "b", ...]

然后,您可以创建一个类变量来跟踪已按下的按钮数量:
var index: Int = 0

然后,在所有按钮都链接到的IBAction中,“touchUpInside”,您可以在每次调用时获取所有这些信息并填充一个标签。当然,这需要将您的按钮标记为0、1、2、3、4(分别对应a到e)。

@IBAction func buttonPressed(sender: UIButton){

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

    index += 1

}

编辑:

看了您的代码后,您应该将其更改为以下内容:

 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

}

对于索引变量和数组,请确保在任何函数之外初始化它们。将它们作为类变量初始化,放置在标签和按钮旁边,如下所示:
@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"]

然后您需要将其他方法更改为以下内容:
@IBAction func LetterOneButtonPressed(sender: UIButton) {
    //you can actually omit the switch statement in your case
    labels[index].text = letters[sender.tag]

    index += 1
}

记住,您需要将每个按钮链接到上述方法。不要为每个按钮使用不同的方法!

谢谢,我会立即尝试并告诉您结果的! - j___.___j
它不起作用(但我认为实际上是我做错了)。你能再帮我一下吗?我是编程新手 :( 这是现在的样子 - j___.___j
当然,你能否在你的问题中发布实现代码?这样我就可以尝试找出问题所在。 - Benjamin Lowry
更少的错误,但仍有一个:“无法在属性初始化程序中使用实例成员'WordLetterOne'; 属性初始化程序在'self'可用之前运行”,出现在“var labels:”的定义中。 - j___.___j
@Janninho 好的,那是因为 Swift 不喜欢你初始化一个还没有加载的对象数组。为了解决这个问题,只需用一个空数组初始化标签数组,然后在 viewDidLoad 方法中设置值即可。我已经更新了代码以反映这一点。试试看吧。 - Benjamin Lowry
显示剩余4条评论

0
希望这对你有所帮助。
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"
        }

    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接