I have a SwiftUI form that will have multiple pickers on the screen. The first one being select a team from a database stored in Core Data. Each of these teams have a one-to-many relationship with Player. Here are the entities:
Player
Team
In my content view I use a fetch request to get all the teams from Core Data, which I will then iterate through to finish my picker.
@FetchRequest(sortDescriptors: [])
private var teams: FetchedResults<Team>
@State var teamSelected = 0
var body: some View {
NavigationView{
Form{
Section(header: Text("Team Selection")){
Picker(selection: $teamSelected, label: Text("Team Select")){
ForEach(0..<teams.count){ team in
Text("\(teams[team].name ?? "Unknown")")
}
}
}
}
.navigationBarTitle("Team Selection")
}
}
I then want to have multiple pickers below this allowing you to select an individual player from that specific team. I have tried a few ways but never found the correct solution and they all seem way too long winded and hacky. My attempt is below
Picker(selection: $number1, label: Text("#1")){
ForEach(0..<teams[teamSelected].people!.count){player in
Text(player.name)
}
}
Any ideas? I want the second picker to be easily replicable, as I will eventually need it a lot