Kim Derek Chihoon
Kim Derek Chihoon

Reputation: 97

Deselect all other Button selection if new one is selected

I have this code :

import SwiftUI
struct PlayButton: View {
    @Binding var isClicked: Bool

    var body: some View {
        Button(action: {
            self.isClicked.toggle()
        }) {
            Image(systemName: isClicked ? "checkmark.circle.fill" : "circle")
        }
    }
}

struct ContentView: View {
    @State private var isPlaying: Bool = false
    var players : [String] = ["Crown" , "King" , "Queen" , "Prince"]
    
    var body: some View {
        VStack {
          ForEach(players, id: \.self) { player in
            HStack {
                Text(player)
                PlayButton(isClicked: $isPlaying)
            }
           }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}

I want to deselect all other previously selected buttons if i select a new one. For example , if i select King and select queen , then King is deselected. How can i do that

What i have done. I honestly could not come with a solution .

Upvotes: 0

Views: 1339

Answers (2)

Jonathan Brown
Jonathan Brown

Reputation: 41

I understand this might look like a lot more code to provide the answer but my assumption is you are trying to make a real world app. A real world app should be testable and so my answer is coming from a place where you can test your logic separate from your UI. This solution allows you to use the data to drive what your view is doing from a model perspective.

import SwiftUI
class PlayerModel {
    let name: String
    var isSelected : Bool = false
    init(_ name: String){
        self.name = name
    }
}
class AppModel: ObservableObject {
    let players : [PlayerModel] = [PlayerModel("Crown") , PlayerModel("King") ,PlayerModel("Queen") ,PlayerModel("Prince")]
    var activePlayerIndex: Int?
    init(){

    }
    func selectPlayer(_ player: PlayerModel){
        players.forEach{
                $0.isSelected = false
        }
        player.isSelected = true
        objectWillChange.send()
    }
}
struct PlayButton: View {
    let isSelected: Bool
    let action : ()->Void

    var body: some View {
        Button(action: {
            self.action()
        }) {
            Image(systemName: isSelected ? "checkmark.circle.fill" : "circle")
        }
    }
}

struct ContentView: View {
    @ObservedObject var model = AppModel()
    var body: some View {
        VStack {
            ForEach(model.players, id: \.name) { player in
                HStack {
                    Text(player.name)
                    PlayButton(isSelected: player.isSelected, action: { self.model.selectPlayer(player) })
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        PlayerView()
    }
}

Upvotes: 1

Raja Kishan
Raja Kishan

Reputation: 18924

For a single selection, at a time you can pass selectedData to PlayButton view

struct PlayButton: View {

    @Binding var selectedData: String
    var data: String

    var body: some View {
        Button(action: {
            selectedData = data
        }) {
            Image(systemName: data == selectedData ? "checkmark.circle.fill" : "circle")
        }
    }
}

struct ContentView: View {
    @State private var selectedPlayer: String = ""
    private var players : [String] = ["Crown" , "King" , "Queen" , "Prince"]

    var body: some View {
        VStack {
            ForEach(players.indices) { index in
                let obj = players[index]
                HStack {
                    Text(obj)
                    PlayButton(selectedData: $selectedPlayer, data: obj)
                }
            }
        }
    }
}

Upvotes: 0

Related Questions