jeudi 7 janvier 2021

Limit selection only to one item based on flag

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 if KING is the in the list of players , there is only one selection allowed and no other selection allowed . So basically , you can only select one player if King is in the list and unable to select another item in the list otherwise you can select as much selection as you like.

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




Aucun commentaire:

Enregistrer un commentaire