Swift 3中的自定义复选框 [英] Custom checkbox in swift 3
问题描述
快速入门
我在视图控制器上有两个复选框按钮,当单击单个复选框按钮(单个)时,选中和取消选中都可以,但是
的目的是当我选中 PICK复选框时,取消选中 Drop Check Box
I had two check box buttons on view controller, when click single check box button(Indvidual) it's check and uncheck working fine, but my aim is when I check "PICK" checkBox button uncheck the "Drop Check Box"
如果我可以选择PickUP checkBox,则应该取消选中
,如果选择Drop CheckBox PickUP,则应该取消选中
,但同时未选择
if I can select PickUP checkBox Drop should uncheck if select Drop CheckBox PickUP Should uncheck but its not happening its selecting Both
这是我的单张支票代码
@IBAction func PushButtonClick(_ sender: UIButton) {
let buttontag = sender.tag
if isChecked {
sender.setImage(UIImage(named:"check1"), for: .normal)
print("checked")
isChecked = false
}
else {
sender.setImage( UIImage(named:"uncheck1"), for: .normal)
isChecked = true
print("uncheck")
}
}
@IBAction func DropButtonClick(_ sender: UIButton) {
if isChecked {
sender.setImage(UIImage(named:"check1"), for: .normal)
print("checked")
isChecked = false
}
else {
sender.setImage( UIImage(named:"uncheck1"), for: .normal)
isChecked = true
print("uncheck")
}
}
如果一个复选框选中另一个复选框,则未选中如何解决此问题……..
if one check box check check other check box Is unchecked how to over come this problem........
推荐答案
步骤1
创建一个用于存储的按钮阵列按钮
create one button array for store the buttons
var buttonscheck = [UIButton]()
步骤2
页面加载将按钮添加到该数组中,例如
on page load add the buttons to that array, for e.g
override func viewDidLoad() {
super.viewDidLoad()
buttonscheck.append(btnPick)
buttonscheck.append(btnDrop)
}
以及为两个按钮创建通用功能,例如
as well as create the common func for both buttons, for e.g
@IBAction func PushButtonClick(_ sender: UIButton) {
第3步
在分配支票之前,请删除先前选择的UIbutton,例如
before assign the check remove the previous selection of your UIbuttons, for e.g
@IBAction func PushButtonClick(_ sender: UIButton) {
// clear previous selection of your buttons
for getbutton in buttonscheck {
getbutton.setImage( UIImage(named:"uncheck1"), for: .normal)
}
// finally set the selected image for your button and it will hold the current button
sender.setImage(UIImage(named:"check1"), for: .normal)
}
这篇关于Swift 3中的自定义复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!