ios - Swift:如何根据按钮点击将结果显示到标签

标签 ios swift uibutton

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

相关文章:

iphone - 在后台 iOS 中录制分块音频

iphone - UIWebView 中的字体显示不正确

ios - Swift Playgrounds 程序中的程序约束问题

ios - UITableViewCell 的 UIButton subview 取消选择 UITableViewCell?

ios - 检查从另一个函数单击了哪个 UIButton

ios - 卓悦网络

ios - 运行旧的 iOS 版本 XCode

swift - TestFlight:没有 SKSpriteNodes

swift - 如何从 swift 3 将 x-www-form-urlencoded 发布到 REST api

swift - 如何在 Swift 中检测 Apple TV 上的按钮突出显示