需要修复我的复选框:在一次单击而不是两次单击中更改状态。Swift 3,IOS
我有多个复选框可以正常工作。 它的工作方式是,有两个图像(一个复选框的图像或一个未选中框的图像)显示或消失在我的按钮中,基于单击该按钮 由于某些原因,当我第一次单击复选框时,它工作正常(将其状态更改为:选中或未选中-单击一次),但当我尝试第二个、第三个或第四个(等)复选框时,需要单击两次才能更改其状态(选中/未选中) 这对用户来说既烦人又混乱。这有什么办法吗 以下是我最后的3个复选框:需要修复我的复选框:在一次单击而不是两次单击中更改状态。Swift 3,IOS,ios,checkbox,swift3,Ios,Checkbox,Swift3,我有多个复选框可以正常工作。 它的工作方式是,有两个图像(一个复选框的图像或一个未选中框的图像)显示或消失在我的按钮中,基于单击该按钮 由于某些原因,当我第一次单击复选框时,它工作正常(将其状态更改为:选中或未选中-单击一次),但当我尝试第二个、第三个或第四个(等)复选框时,需要单击两次才能更改其状态(选中/未选中) 这对用户来说既烦人又混乱。这有什么办法吗 以下是我最后的3个复选框: /////Checkboxes @IBOutlet weak var Box49: UIButton! @
/////Checkboxes
@IBOutlet weak var Box49: UIButton!
@IBOutlet weak var Box50: UIButton!
@IBOutlet weak var Box51: UIButton!
var BoxON = UIImage(named: "CheckBox")
var BoxOFF = UIImage(named:"UnCheckBox")
var isBoxClicked: Bool!
override func viewDidLoad() {
super.viewDidLoad()
isBoxClicked = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func Box49(_ sender: Any) {
if isBoxClicked == true{
isBoxClicked = false
}else{
isBoxClicked = true
}
if isBoxClicked == true{
Box49.setImage(BoxON, for: UIControlState.normal)
}else{
Box49.setImage(BoxOFF, for: UIControlState.normal)
}
}
@IBAction func Box50(_ sender: Any) {
if isBoxClicked == true{
isBoxClicked = false
}else{
isBoxClicked = true
}
if isBoxClicked == true{
Box50.setImage(BoxON, for: UIControlState.normal)
}else{
Box50.setImage(BoxOFF, for: UIControlState.normal)
}
}
@IBAction func Box51(_ sender: Any) {
if isBoxClicked == true{
isBoxClicked = false
}else{
isBoxClicked = true
}
if isBoxClicked == true{
Box51.setImage(BoxON, for: UIControlState.normal)
}else{
Box51.setImage(BoxOFF, for: UIControlState.normal)
}
}
谢谢,Dan您正在使用相同实例属性的问题
isBoxClicked
with all button,而不是您需要将图像设置为状态正常和选中的所有按钮,然后在按钮操作中更改其选中状态。
也可以更改按钮出口名称或操作名称,因为它们都是相同的。所以应该是这样
@IBOutlet var box49: UIButton!
@IBOutlet var box50: UIButton!
@IBOutlet var box51: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
box49.setImage(BoxOFF, for: .normal)
box49.setImage(BoxON, for: .selected)
box50.setImage(BoxOFF, for: .normal)
box50.setImage(BoxON, for: .selected)
box51.setImage(BoxOFF, for: .normal)
box51.setImage(BoxON, for: .selected)
}
@IBAction func boxButton(_ sender: Button) {
sender.isSelected = !sender.isSelected
}
现在这样设置按钮动作
@IBAction func box49Button(_ sender: Button) {
sender.isSelected = !sender.isSelected
}
@IBAction func box50Button(_ sender: Button) {
sender.isSelected = !sender.isSelected
}
@IBAction func box51Button(_ sender: Button) {
sender.isSelected = !sender.isSelected
}
或者,您可以添加单个按钮操作并将该操作设置为所有三个按钮,而不是像这样为每个按钮设置三个不同的操作
@IBOutlet var box49: UIButton!
@IBOutlet var box50: UIButton!
@IBOutlet var box51: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
box49.setImage(BoxOFF, for: .normal)
box49.setImage(BoxON, for: .selected)
box50.setImage(BoxOFF, for: .normal)
box50.setImage(BoxON, for: .selected)
box51.setImage(BoxOFF, for: .normal)
box51.setImage(BoxON, for: .selected)
}
@IBAction func boxButton(_ sender: Button) {
sender.isSelected = !sender.isSelected
}
您使用相同实例属性的问题是,单击了所有按钮,而不是需要将图像设置为“正常”和“选定”状态下的所有按钮,然后在按钮操作中更改其选定状态。 也可以更改按钮出口名称或操作名称,因为它们都是相同的。所以应该是这样
@IBOutlet var box49: UIButton!
@IBOutlet var box50: UIButton!
@IBOutlet var box51: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
box49.setImage(BoxOFF, for: .normal)
box49.setImage(BoxON, for: .selected)
box50.setImage(BoxOFF, for: .normal)
box50.setImage(BoxON, for: .selected)
box51.setImage(BoxOFF, for: .normal)
box51.setImage(BoxON, for: .selected)
}
@IBAction func boxButton(_ sender: Button) {
sender.isSelected = !sender.isSelected
}
现在这样设置按钮动作
@IBAction func box49Button(_ sender: Button) {
sender.isSelected = !sender.isSelected
}
@IBAction func box50Button(_ sender: Button) {
sender.isSelected = !sender.isSelected
}
@IBAction func box51Button(_ sender: Button) {
sender.isSelected = !sender.isSelected
}
或者,您可以添加单个按钮操作并将该操作设置为所有三个按钮,而不是像这样为每个按钮设置三个不同的操作
@IBOutlet var box49: UIButton!
@IBOutlet var box50: UIButton!
@IBOutlet var box51: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
box49.setImage(BoxOFF, for: .normal)
box49.setImage(BoxON, for: .selected)
box50.setImage(BoxOFF, for: .normal)
box50.setImage(BoxON, for: .selected)
box51.setImage(BoxOFF, for: .normal)
box51.setImage(BoxON, for: .selected)
}
@IBAction func boxButton(_ sender: Button) {
sender.isSelected = !sender.isSelected
}
您正在为所有按钮使用
isBoxClicked
变量的一个实例。您应该创建一个数组isBoxClicked
。通过使用数组,您可以进一步检查哪个复选框(UIButon
)是未选中的复选框
var isBoxClickedArray:[int] = {0,0}
首先为获取按钮索引创建枚举
enum ButtonIndex{
case Box49
case Box50
}
将枚举值指定为按钮中的标记
Box49Button.tag = ButtonIndex.Box49.rawValue
Box50Button.tag = ButtonIndex.Box50.rawValue
为获取操作创建一个方法
@IBAction func actionType(_ sender: Any) {
let button = sender as! UIBarButton
if ( button.tag == ButtonIndex.BOX49.rawValue ){
print("Box49 button pressed")
if ( isBoxClicked[ButtonIndex.BOX49.rawValue] == 0 ){
print("box49 is unchecked")
isBoxClicked[ButtonIndex.BOX49.rawValue] = 1// change it's value to checked
// change image also
}else{
// box is checked
isBoxClicked[ButtonIndex.BOX49.rawValue] = 0
// change value in array and image also
}
}else{
print("Box50 button pressed")
}
}
更新
在viewcontroller中声明数组。如果您使用的是tableview,请在ViewDiLoad或CellForRowatineXpath中指定tag on按钮。对于enum,如下声明它们
import UIKit
enum indexs : Int{
case first
case second
}
class ViewController: UIViewController {
您正在为所有按钮使用
isBoxClicked
变量的一个实例。您应该创建一个数组isBoxClicked
。通过使用数组,您可以进一步检查哪个复选框(UIButon
)是未选中的复选框
var isBoxClickedArray:[int] = {0,0}
首先为获取按钮索引创建枚举
enum ButtonIndex{
case Box49
case Box50
}
将枚举值指定为按钮中的标记
Box49Button.tag = ButtonIndex.Box49.rawValue
Box50Button.tag = ButtonIndex.Box50.rawValue
为获取操作创建一个方法
@IBAction func actionType(_ sender: Any) {
let button = sender as! UIBarButton
if ( button.tag == ButtonIndex.BOX49.rawValue ){
print("Box49 button pressed")
if ( isBoxClicked[ButtonIndex.BOX49.rawValue] == 0 ){
print("box49 is unchecked")
isBoxClicked[ButtonIndex.BOX49.rawValue] = 1// change it's value to checked
// change image also
}else{
// box is checked
isBoxClicked[ButtonIndex.BOX49.rawValue] = 0
// change value in array and image also
}
}else{
print("Box50 button pressed")
}
}
更新
在viewcontroller中声明数组。如果您使用的是tableview,请在ViewDiLoad或CellForRowatineXpath中指定tag on按钮。对于enum,如下声明它们
import UIKit
enum indexs : Int{
case first
case second
}
class ViewController: UIViewController {
我该怎么写全文呢?我总是出错。我不确定我做错了什么here@DanielG您遇到的错误有哪些?如果可能,请用您尝试过的代码编辑您的问题,并显示错误的屏幕截图。@DanielG您需要在ViewDilload中设置此图像,再次检查我的答案您正在
didReceiveMemoryWarning
中设置图像。UIButton类型的值没有我在另一篇文章中设置的成员集图像!:)我该怎么写全文呢?我总是出错。我不确定我做错了什么here@DanielG您遇到的错误有哪些?如果可能,请用您尝试过的代码编辑您的问题,并显示错误的屏幕截图。@DanielG您需要在ViewDilload中设置此图像,再次检查我的答案您正在didReceiveMemoryWarning
中设置图像。UIButton类型的值没有我在另一篇文章中设置的成员集图像!:)我似乎不能把它全部写在一起。我有点迷路了。如何获得预期的声明(对于您的var)或预期的表达式(对于您的enum)?我似乎无法将其全部写在一起。我有点迷路了。如何获得预期的声明(对于var)或预期的表达式(对于enum)?