Swift:如何根据按钮点击将结果显示到Label

时间:2016-08-28 03:49:23

标签: ios swift uibutton

我的ViewControllers中有四个按钮作为复选框。当用户单击复选框按钮并单击下面的提交按钮时,我想根据所选复选框的数量将结果显示给Label。例如,如果选中了两个复选框,则标签文本将被" 50%选中"。有人可以帮助我,我该怎么办。

2 个答案:

答案 0 :(得分:0)

非常确定您使用UIButtons作为复选框,您可以检查四个按钮的选定状态,并显示标签文本。 UIButtons有一个isSelected属性。 希望这有帮助

答案 1 :(得分:0)

// 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

}