为动态UIButton分配标签

时间:2018-02-23 10:39:08

标签: ios swift uibutton tags

我在http://helpmecodeswift.com/advanced-functions/generating-uibuttons-loop

找到了UIButton的动态视图示例

我将此代码插入到我的应用中,但遗憾的是我无法找到为每个按钮设置标记的方法 我的目标是知道点击了哪个按钮,计算每个按钮的点击次数并将此信息保存到我的数据库中。

override func viewDidLoad() {
    super.viewDidLoad()

    var arrayOfVillains = ["santa", "bugs", "superman", "batman"]

    var buttonY: CGFloat = 20
    for villain in arrayOfVillains {

        let villainButton = UIButton(frame: CGRect(x: 50, y: buttonY, width: 250, height: 30))
        buttonY = buttonY + 50  // we are going to space these UIButtons 50px apart

        villainButton.layer.cornerRadius = 10  // get some fancy pantsy rounding
        villainButton.backgroundColor = UIColor.darkGrayColor()
        villainButton.setTitle("Button for Villain: \(villain)", forState: UIControlState.Normal) // We are going to use the item name as the Button Title here.
        villainButton.titleLabel?.text = "\(villain)"
        villainButton.addTarget(self, action: "villainButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(villainButton)  // myView in this case is the view you want these buttons added
    }
}

func villainButtonPressed(sender:UIButton!) {

    if sender.titleLabel?.text != nil {
        println("You have chosen Villain: \(sender.titleLabel?.text)")
    } else {

        println("Nowhere to go :/")

    }

}

}

那么如何设置和获取代码中每个按钮的标记(在本例中)? 提前谢谢!

2 个答案:

答案 0 :(得分:0)

你应该为此使用基于整数的for循环。

for i in 0 ..< arrayOfVillains.count {
    let villain = arrayOfVillains[i]
    let villainButton = UIButton(frame: CGRect(x: 50, y: buttonY, width: 250, height: 30))
    villainButton.tag = i
}

答案 1 :(得分:0)

您可以使用枚举数组来访问迭代中当前项的索引,并将其用作标记:

override func viewDidLoad() {
    super.viewDidLoad()

    var arrayOfVillains = ["santa", "bugs", "superman", "batman"]

    var buttonY: CGFloat = 20  
    for (index, villain) in arrayOfVillains.enumerated() {

        let villainButton = UIButton(frame: CGRect(x: 50, y: buttonY, width: 250, height: 30))
        buttonY = buttonY + 50  // we are going to space these UIButtons 50px apart

        // set the tag to current index
        villainButton.tag = index

        villainButton.layer.cornerRadius = 10  // get some fancy pantsy rounding
        villainButton.backgroundColor = UIColor.darkGrayColor()
        villainButton.setTitle("Button for Villain: \(villain)", forState: UIControlState.Normal) // We are going to use the item name as the Button Title here.
        villainButton.titleLabel?.text = "\(villain)"
        villainButton.addTarget(self, action: "villainButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(villainButton)  // myView in this case is the view you want these buttons added
        }
    }       
}

@objc func villainButtonPressed(sender:UIButton!) {

    // tag of pressed button
    print(">>> you pressed button with tag: \(sender.tag)")

    if sender.titleLabel?.text != nil {
        println("You have chosen Villain: \(sender.titleLabel?.text)")
    } else {

        println("Nowhere to go :/")

    }        
}