如何从单选按钮获取值到 swift 5 中的变量
how to get value from radio button to a variable in swift 5
Need to save ther value from the radio button to a variable
@IBOutlet weak var userButton: UIButton!
@IBOutlet weak var propertyButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func btClicked (sender: UIButton) {
let buttonArray = [userButton , propertyButton]
buttonArray.forEach {_ in
[=11=]?.isSelected = false
}
}
创建一个变量来存储按钮的值
var strVal = ""
为两个按钮创建两个不同的插座
@IBAction func btnUserClicked(_ sender: UIButton) {
setValue(isUser: true)
}
@IBAction func btnPropertyOwnerClicked(_ sender: UIButton) {
setValue(isUser: false)
}
setValue(isUser : Bool) 方法将存储您选择的按钮值并根据选择更改按钮的图像
func setValue(isUser : Bool){
if isUser{
strVal = "User"
btnUser.setImage(UIImage(named: "circle-fill"), for: .normal)
btnPropertyOwner.setImage(UIImage(named: "circle"), for: .normal)
}
else{
strVal = "Property Owner"
btnPropertyOwner.setImage(UIImage(named: "circle-fill"), for: .normal)
btnUser.setImage(UIImage(named: "circle"), for: .normal)
}
}
图像 'circle' 是按钮上已有的默认图像,图像 'circle-fill' 用于指示您的按钮是否被选中。
或者您可以使用 sender.tag 属性 按钮:
@IBAction func btnUserClicked(_ sender: UIButton) {
//setValue(isUser: true)
if(sender.tag == 0){
strVal = "user"
btnUser.setImage(UIImage(named: "circle-fill"), for: .normal)
btnPropertyOwner.setImage(UIImage(named: "circle"), for: .normal)
}
else{
strVal = "property user"
btnPropertyOwner.setImage(UIImage(named: "circle-fill"), for: .normal)
btnUser.setImage(UIImage(named: "circle"), for: .normal)
}
}
Need to save ther value from the radio button to a variable
@IBOutlet weak var userButton: UIButton!
@IBOutlet weak var propertyButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func btClicked (sender: UIButton) {
let buttonArray = [userButton , propertyButton]
buttonArray.forEach {_ in
[=11=]?.isSelected = false
}
}
创建一个变量来存储按钮的值
var strVal = ""
为两个按钮创建两个不同的插座
@IBAction func btnUserClicked(_ sender: UIButton) {
setValue(isUser: true)
}
@IBAction func btnPropertyOwnerClicked(_ sender: UIButton) {
setValue(isUser: false)
}
setValue(isUser : Bool) 方法将存储您选择的按钮值并根据选择更改按钮的图像
func setValue(isUser : Bool){
if isUser{
strVal = "User"
btnUser.setImage(UIImage(named: "circle-fill"), for: .normal)
btnPropertyOwner.setImage(UIImage(named: "circle"), for: .normal)
}
else{
strVal = "Property Owner"
btnPropertyOwner.setImage(UIImage(named: "circle-fill"), for: .normal)
btnUser.setImage(UIImage(named: "circle"), for: .normal)
}
}
图像 'circle' 是按钮上已有的默认图像,图像 'circle-fill' 用于指示您的按钮是否被选中。
或者您可以使用 sender.tag 属性 按钮:
@IBAction func btnUserClicked(_ sender: UIButton) {
//setValue(isUser: true)
if(sender.tag == 0){
strVal = "user"
btnUser.setImage(UIImage(named: "circle-fill"), for: .normal)
btnPropertyOwner.setImage(UIImage(named: "circle"), for: .normal)
}
else{
strVal = "property user"
btnPropertyOwner.setImage(UIImage(named: "circle-fill"), for: .normal)
btnUser.setImage(UIImage(named: "circle"), for: .normal)
}
}