如何更改带有数组的一组按钮的标题?

时间:2018-12-12 23:12:28

标签: ios arrays swift

  • 我有两个IBAction组,每个组三个按钮。
  • 每个按钮都有一个不同的标记-在每个组中:1、2和3。
  • 我有三个数组,每个数组三个值。

我想按下第一个IBAction的第一个按钮,并根据数组组更改第二个IBAction中的三个按钮的标题。

代码如下:

import UIKit

class ViewController: UIViewController {

var firstArray = ["A1","A2","A3"]
var secondArray = ["B1","B2","B3"]
var thirdArray = ["C1","C2","C3"]

override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction func greenButtons(_ sender: UIButton) {
    if sender.tag == 1 {
        //change the title of the grayButtons with the firstArray         
}
    if sender.tag == 2 {
        //change the title of the grayButtons with the secondArray         
}
    if sender.tag == 3 {
        //change the title of the grayButtons with the thirdArray         
}

}

@IBAction func grayButtons(_ sender: UIButton) {
}

}

1 个答案:

答案 0 :(得分:0)

您可以尝试

// create outlet group for each
@IBOutlet weak var greenButtons:[UIButton]! 
@IBOutlet weak var grayButtons:[UIButton]!

greenButtons的内部动作

grayButtons.forEach { $0.setTitle(firstArray[$0.tag - 1],for:.normal) }

代替检查,您可以将数组构造为

let allArr = [  ["A1","A2","A3"],  ["B1","B2","B3"],["C1","C2","C3"]]

然后

grayButtons.forEach { $0.setTitle(allArr[sender.tag - 1][$0.tag - 1],for:.normal) }