gpt4 book ai didi

Swift UIControlState.Selected 保持选中状态吗?

转载 作者:行者123 更新时间:2023-11-30 10:09:50 26 4
gpt4 key购买 nike

我有 3 个按钮,提供给用户选择他们希望查看的类别。

它们的定义如下:

 //outlets
@IBOutlet weak var ChineseButton: UIButton!
@IBOutlet weak var IndianButton: UIButton!
@IBOutlet weak var MexicanButton: UIButton!

我对这些按钮执行以下操作(将原始图像按钮更改为灰色版本):

// Action

@IBAction func ChineseButtonPressed(sender: UIButton)
{

ChineseButton.setImage(UIImage(named: "chinese.png"), forState: UIControlState.Normal)
ChineseButton.setImage(UIImage(named: "chinese.png"), forState: UIControlState.Selected)
IndianButton.setImage(UIImage(named: "grey-indian.png"), forState: UIControlState.Normal)
MexicanButton.setImage(UIImage(named: "grey-mexican.png"), forState: UIControlState.Normal)

}
@IBAction func EightFacedButtonPressed(sender: UIButton) {
IndianButton.setImage(UIImage(named: "indian.png"), forState: UIControlState.Normal)
IndianButton.setImage(UIImage(named: "indian.png"), forState: UIControlState.Selected)
ChineseButton.setImage(UIImage(named: "grey-chinese.png"), forState: UIControlState.Normal)

MexicanButton.setImage(UIImage(named: "grey-mexican.png"), forState: UIControlState.Normal)
}

@IBAction func TweleveFacedButtonPressed(sender: UIButton) {
MexicanButton.setImage(UIImage(named: "mexican.png"), forState: UIControlState.Normal)
MexicanButton.setImage(UIImage(named: "mexican.png"), forState: UIControlState.Selected)
ChineseButton.setImage(UIImage(named: "grey-chinese.png"), forState: UIControlState.Normal)
IndianButton.setImage(UIImage(named: "grey-indian.png"), forState: UIControlState.Normal)
}

此操作函数还确定 UIControlState,我有另一个按钮,单击该按钮后会将您带到另一个页面,但需要了解选择了哪个按钮。

我尝试了以下操作,但它似乎无法识别选择了哪个按钮?

 @IBAction func GenerateButton(sender: UIButton) {
if ChineseButton.selected == true{
println("Chinese button was selected")
}

我是 swift 新手,因此我们将不胜感激!

谢谢:)

最佳答案

您没有选择按钮,您只是更改图像。

首先,您可以简化所有代码并设置一次图像,灰色表示正常,彩色表示选定状态:

override func viewDidLoad() {
super.viewDidLoad()

MexicanButton.setImage(UIImage(named: "mexican.png"), forState: UIControlState.Normal)
MexicanButton.setImage(UIImage(named: "grey-mexican.png"), forState: UIControlState.Selected)
ChineseButton.setImage(UIImage(named: "grey-chinese.png"), forState: UIControlState.Normal)
ChineseButton.setImage(UIImage(named: "chinese.png"), forState: UIControlState.Selected)
IndianButton.setImage(UIImage(named: "grey-indian.png"), forState: UIControlState.Normal)
IndianButton.setImage(UIImage(named: "indian.png"), forState: UIControlState.Selected)
}

您也可以在 Storyboard 上进行配置,在图标的不同 State Configs 处更改 Image:

enter image description here

然后,在每个操作中,您只需选择按钮,就会显示正确的图像(您可以对所有按钮使用相同的操作):

@IBAction func AnyButtonPressed(sender: UIButton) {
// Deselect all buttons but the pressed one
for button in [ChineseButton, IndianButton, MexicanButton] {
button.selected = sender === button
}
}

关于Swift UIControlState.Selected 保持选中状态吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33570981/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com