gpt4 book ai didi

ios - UIButton选择状态并存储值

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

嘿,我是 iOS 开发新手,我有一个问题,让两个按钮处于选中和未选中状态,然后存储性别值。我希望当用户选择性别时,其他性别将被禁用。谁能帮我吗?

我的按钮代码

let menButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Men", for: .normal)
button.setTitleColor(.white, for: .normal)
button.backgroundColor = .yellow
button.isSelected = false
button.isEnable = true
button.addTarget(self, action: #selector(handlePickGender), for: .touchUpInside
return button
}()

let womenButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Women", for: .normal)
button.setTitleColor(.white, for: .normal)
button.backgroundColor = .blue
button.isSelected = false
button.isEnable = true
button.addTarget(self, action: #selector(handlePickGender), for: .touchUpInside
return button
}()

最佳答案

试试这个:

@objc func handlePickGender(sender: UIButton) {
menButton.setTitleColor(UIColor.yellow, for: .normal)
womenButton.setTitleColor(UIColor.yellow, for: .normal)
sender.setTitleColor(UIColor.blue, for: .normal)
//store gender
let selectedGender = sender.title(for: .normal)
}

关于ios - UIButton选择状态并存储值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52889333/

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