gpt4 book ai didi

ios - swift 3 : Add text to label if button is pressed

转载 作者:可可西里 更新时间:2023-11-01 02:14:12 26 4
gpt4 key购买 nike

在我的 Xcode 项目中,我有五个空文本标签(即 I、II、III、IV、V)和五个按钮,每个按钮代表一个字母(即 a、b、c、d、e)。

我想用字母填充文本标签 I 到 V,但按字母按钮按下的顺序(即用户按下 c → 标签 < strong>I 包含字母 c;然后用户按下 a → 标签 II 包含字母 a 并且等等)。

你能给我任何提示/解决方案吗?

谢谢,J.

编辑:根据本杰明的建议,我现在做了以下工作:

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) {
}

}

最佳答案

我的建议是将您的标签和字母放在一个数组中:

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

然后您可以创建一个类变量来跟踪按下了多少个按钮:

var index: Int = 0

然后在所有按钮都链接到“touchUpInside”的 IBAction 中,您可以获取所有这些信息并在每次调用时填充一个标签。这当然需要您将按钮的标签设置为 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
}

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

关于ios - swift 3 : Add text to label if button is pressed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39684669/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com