我的 ViewController 中有四个按钮,它们充当复选框。当用户单击复选框按钮并单击下面的提交按钮时,我想根据所选复选框的数量将结果显示到标签。例如,如果选中两个复选框,则标签文本将为“50% selected”。有人可以帮我看看我该怎么做吗?
最佳答案
// To maintain count of number of checkbox buttons selected
var checkCount: Int = 0
@IBAction func CheckBox_1_Selected(sender: UIButton) {
if (sender.selected)
{
// If CheckBox_1 already selected, then decrement count
checkCount = checkCount - 1
}
else
{
// If CheckBox_1 selected, then increment count
checkCount = checkCount + 1
}
}
@IBAction func CheckBox_2_Selected(sender: UIButton) {
if (sender.selected)
{
// If CheckBox_2 already selected, then decrement count
checkCount = checkCount - 1
}
else
{
// If CheckBox_2 selected, then increment count
checkCount = checkCount + 1
}
}
@IBAction func CheckBox_3_Selected(sender: UIButton) {
if (sender.selected)
{
// If CheckBox_3 already selected, then decrement count
checkCount = checkCount - 1
}
else
{
// If CheckBox_3 selected, then increment count
checkCount = checkCount + 1
}
}
@IBAction func CheckBox_4_Selected(sender: UIButton) {
if (sender.selected)
{
// If CheckBox_4 already selected, then decrement count
checkCount = checkCount - 1
}
else
{
// If CheckBox_4 selected, then increment count
checkCount = checkCount + 1
}
}
@IBAction func submitAction() {
countLabel.text = NSString(format:" %d % selected", checkCount * 25)
checkCount = 0
}
关于ios - Swift:如何根据按钮点击将结果显示到标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39187838/