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