SwiftUI 3 个按钮 - 只能同时切换一个按钮

SwiftUI 3 buttons - Only one button should be toggled at the same time

所以我创建了 3 个按钮。他们在切换为 true 时添加点,或在按钮切换再次设置为 false 时删除点。

现在我的问题是: 如何确保只有一个按钮能够切换为 True? 因此,例如,当我按下“是”按钮时,它会变成绿色并添加点数。但是当我下定决心,并将我的选择更改为“有时”按钮时,“是”按钮应该切换回 false,只有“有时”应该切换为 true。

我为此苦思冥想了很长一段时间。有人可以帮我解决这个问题吗?


               Button("Yes") {
                    ShowButton.toggle()
                    ButtonYes.toggle()
                    if ButtonYes == true {
                    Xcode += 5
                        } else { ButtonYes = false
                        Xcode -= 5
                        }
                    if ButtonYes == true {
                    Ycode += 5
                        } else { ButtonYes = false
                        Ycode -= 5
                        }
                }
                .frame(width: 50, height: 50, alignment: .center)
                .foregroundColor(.white)
                .padding()
                .background(ButtonYes ? Color(red: 0.272, green: 0.471, blue: 0.262) : Color(red: 0.493, green: 0.184, blue: 0.487))
                .cornerRadius(20)
                .shadow(color: .black, radius: 10, x: 10, y: 10)
                   
                    Button("No") {
                        ShowButton.toggle()
                        ButtonNo.toggle()
                        if ButtonNo == true {
                        Xcode += 3
                            } else { ButtonNo = false
                            Xcode -= 3
                            }
                        if ButtonNo == true {
                        Ycode += 3
                            } else { ButtonYes = false
                            Ycode -= 3
                            }
                        
                        
                    }
                    .frame(width: 50, height: 50, alignment: .center)
                    .foregroundColor(.white)
                    .padding()
                    .background(ButtonNo ? Color(red: 0.272, green: 0.471, blue: 0.262) : Color(red: 0.493, green: 0.184, blue: 0.487))
                    .cornerRadius(20)
                    .shadow(color: .black, radius: 10, x: 10, y: 10)
                    
                    Button("Sometimes") {
                        ShowButton.toggle()
                        ButtonSometimes.toggle()
                        if ButtonSometimes == true {
                        Xcode += 1
                            } else { ButtonSometimes = false
                            Xcode -= 1
                            }
                        if ButtonSometimes == true {
                        Ycode += 1
                            } else { ButtonSometimes = false
                            Ycode -= 1
                            }
                      
                        
                    }
                    .frame(width: 50, height: 50, alignment: .center)
                    .foregroundColor(.white)
                    .padding()
                    .background(ButtonSometimes ? Color(red: 0.272, green: 0.471, blue: 0.262) : Color(red: 0.493, green: 0.184, blue: 0.487))
                    .cornerRadius(20)
                    .shadow(color: .black, radius: 10, x: 10, y: 10)
                }  
import UIKit

class ViewController: UIViewController {

@IBOutlet weak var btnSomething: UIButton!
@IBOutlet weak var btnNo: UIButton!
@IBOutlet weak var btnYes: UIButton!

private var btnArry: [UIButton] = []
override func viewDidLoad() {
    super.viewDidLoad()
    btnArry = [btnSomething,btnYes,btnNo]
    // Do any additional setup after loading the view.
}

// MARK:- Toggle
private func toggleButton(_ sender: UIButton) {
    for i in btnArry {
        if i == sender {
            i.backgroundColor = .blue
        } else {
            i.backgroundColor = .white
        }
    }
}

@IBAction func btnYes(_ sender: UIButton) {
    toggleButton(sender)
}

@IBAction func btnNo(_ sender: UIButton) {
    toggleButton(sender)
}
@IBAction func btnSomething(_ sender: UIButton) {
    toggleButton(sender)
}
}

您应该在每次需要时使用状态重新呈现您的按钮。这是我根据您的代码编写的代码,但使用了 @State 包装器。

enum Options {
    case yes, no, sometimes
}

struct TestButtonView: View {
    
    // Selected option state
    @State private var selectedOption: Options?
    // Answer state, that we injecting in view init
    @State var answer: Options
    // Score
    @State var Xcode: Int = 0
    @State var Ycode: Int = 0
    
    var body: some View {
        HStack {
            Group {
                Button("Yes") {
                    // Setting selected button
                    selectedOption = .yes
                    
                    // Your score magick code
                    if selectedOption == .yes {
                        Xcode += 5
                    } else {
                        Xcode -= 5
                    }
                    if selectedOption == .yes {
                        Ycode += 5
                    } else {
                        Ycode -= 5
                    }
                }
                .frame(width: 50, height: 50, alignment: .center)
                .padding()
                // Here we make button green if it's correct answer
                .background(Options.yes == answer && selectedOption == .yes ? Color(red: 0.272, green: 0.471, blue: 0.262) : Color(red: 0.493, green: 0.184, blue: 0.487))
                
                Button("No") {
                    // Setting selected button
                    selectedOption = .no
                    
                    // Your score magick code
                    if selectedOption == .no {
                        Xcode += 3
                    } else {
                        Xcode -= 3
                    }
                    if selectedOption == .no {
                        Ycode += 3
                    } else {
                        Ycode -= 3
                    }
                    
                    
                }
                .frame(width: 50, height: 50, alignment: .center)
                .padding()
                // Here we make button green if it's correct answer
                .background(Options.no == answer && selectedOption == .no ? Color(red: 0.272, green: 0.471, blue: 0.262) : Color(red: 0.493, green: 0.184, blue: 0.487))
                
                Button("Sometimes") {
                    // Setting selected button
                    selectedOption = .sometimes
                    
                    // Your score magick code
                    if selectedOption == .sometimes {
                        Xcode += 1
                    } else {
                        Xcode -= 1
                    }
                    if selectedOption == .sometimes {
                        Ycode += 1
                    } else {
                        Ycode -= 1
                    }
                }
                .frame(width: 50, height: 50, alignment: .center)
                .padding()
                // Here we make button green if it's correct answer
                .background(Options.sometimes == answer && selectedOption == .sometimes ? Color(red: 0.272, green: 0.471, blue: 0.262) : Color(red: 0.493, green: 0.184, blue: 0.487))
            }
            // Grouping view for better code readability
            .frame(width: 50, height: 50, alignment: .center)
            .foregroundColor(.white)
            .padding()
            .cornerRadius(20)
            .shadow(color: .black, radius: 10, x: 10, y: 10)
        }
    }
}

// Here preview and you can play with it on canvas
struct TestButtonView_Previews: PreviewProvider {
    static var previews: some View {
        TestButtonView(answer: .yes)
    }
}