Ios 为动态按钮指定标记

Ios 为动态按钮指定标记,ios,swift,uibutton,tags,Ios,Swift,Uibutton,Tags,我在以下位置找到了ui按钮的动态视图示例: 我将此代码插入到我的应用程序中,但不幸的是,我找不到为每个按钮设置标记的方法。 我的目标是知道点击了哪个按钮,计算每个按钮的点击量,并将这些信息保存到我的数据库中 override func viewDidLoad() { super.viewDidLoad() var arrayOfVillains = ["santa", "bugs", "superman", "batman"] var buttonY: CGFloat

我在以下位置找到了
ui按钮的动态视图示例:

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

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 :/")

    }

}

}
那么,如何在代码中为每个按钮设置和获取标签(在本例中)?
提前谢谢你

对此,应使用基于整数的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
}
0中的i的
您可以使用枚举数组访问迭代中当前项的索引,并将其用作标记:

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 :/")

    }        
}
你可以这样做

var buttonTag = 1
for villain in arrayOfVillains {
   let villainButton = UIButton(frame: CGRect(x: 50, y: buttonY, width: 250, height: 30))
   villainButton.tag = buttonTag
   buttonTag += 1
}