UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Setting a picker view default value to object

Forums > SwiftUI

I have a PickerView inside a form that I want to default to a certain value. I pass an object called MatchEvent into the sheet and inside the MatchEvent there is a player attribute. Players are collected inside another class called Team. I have tried to loop through the team attribute players and return and index but I'm thinking there must be a more streamlined way. I want to have a picker view that is already set to the value passed in. Below is my picker view code:

@State var playerSelect = 0

Picker(selection: $playerSelect, label: Text("Select Player")) {
    ForEach(0..<team.players.count) {
        Text("\(team.players[$0].shirtNumber) - \(team.players[$0].playerName)")
    }
     .navigationBarTitle("Select Player")
}
Text("Selected - \(team.players[playerSelect].shirtNumber)")

Here is my team class code:

@Published var players: [Player] = [Player(playerName: "", shirtNumber: 1, active: true),
                                    Player(playerName: "", shirtNumber: 2, active: true),
                                    Player(playerName: "", shirtNumber: 3, active: true),
                                    Player(playerName: "", shirtNumber: 4, active: true), //Carries on for 23 players

Any help would be appreciated, cheers!

3      

Looks like you're missing the tag on each item. I do this in my app, and it works perfectly:

 @State private var exercise: Exercise?
 ....
 ....
Picker("Exercise", selection: $exercise) {
                            List {
                                ForEach(exercises) { exercise in
                                    ExerciseRow(exercise: exercise)
                                        .tag(exercise as Exercise?)
                                }
                            }
                        }

3      

The binding you pass to your picker is the default selection in the picker

Picker(selection: $playerSelect, label: Text("Select Player")) { ... }

Since you have $playerSelect set to 0, the first item in the array will always be the selection.

I am not sure what you want to do, if you want to somehow have a default selection when a team is passed to this new view, where you have the picker, then consider passing in that selection. So if the user taps on a team, it has a specific player selected by default.

For more info, you need to show the entire code. The view that contains the picker and the whole code for Team. Otherwise, we can only provide limited feedback.

3      

This can be accomplished by placing an .onAppear after the brackets of the ForEach loop

Picker(selection: $playerSelect, label: Text("Select Player")) { ForEach(0..<team.players.count) { Text("(team.players[$0].shirtNumber) - (team.players[$0].playerName)") } .onappear { playerSelect = whateverDefaultValueYouWant }

4      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.