I have a multi view app I am trying to develop as my first app. I have gone through much of HackingwithSwiftui
Xcode 16.1 SwiftUI on a MacAir
The prior views in the app store various information. golf Course info in one and Golfers info in another.
This third view is to record rounds of golf played. I started it simple with me entering input manually and that worked.
I then decided to start using pickers with the first two being a DatePicker and the second pulling in the nickname(Handle) for players to select from. in a Handle Picker.
Following is my code. I can select a date as of Now and prior for date played and also the second picker does pull in the all the Handles from my prior view and I can select a Handle of the player for the round.
I then fill in all the other information. When I exit the view I do see that the Round is created but the Date always defaults to Now and the Handle stays blank. When i go back in to edit the round I can change the date and select a Handle but can get them to save.
I have tried many things and searched for days on the web for examples with no luck. I am sure its something simple.
Any help is appreciated as I want to add for pickers for course, tee and other fields. But until I figure out what I am missing the project is at a standstill.
import SwiftUI
import SwiftData
struct RoundsEditDataView: View {
@Bindable var roundsdata: RoundsData
@Environment(.modelContext) private var modelContext
@State private var playDate = Date.now
@Query(sort: \PlayerData.playerHandle) private var players: [PlayerData]
@State private var selectedHandle: PlayerData? = nil
var body: some View {
Form {
HStack {
Text("Course:")
TextField("Course Name", text: $roundsdata.roundscourseName)
.textContentType(.name)
}
HStack {
DatePicker("Date:", selection: $playDate, in: ...Date(),
displayedComponents: .date)
}
Section {
Picker("Handle:", selection: $selectedHandle) {
Text("Select a Handle").tag(nil as PlayerData?)
ForEach(players, id: \.self) { player in
HStack {
Text(player.playerHandle)
.frame(maxWidth: .infinity, alignment: .leading)
.tag(player as PlayerData?)
}
.frame(maxWidth: .infinity, alignment: .leading)
.tag(player as PlayerData?)
}
}
// .pickerStyle(.inline) this does not fix issue or design wise work
}
The rest of this works fine for now until I decide to convert more to picker lists.
HStack {
Text("Tee:")
TextField("Tee", text: $roundsdata.roundsTee)
.textContentType(.name)
}
HStack {
Text("Handicap:")
TextField("Handicap", value: $roundsdata.roundsHandicap, format: .number)
.textContentType(.name)
}
HStack {
Text("*****:")
TextField("*****", value: $roundsdata.roundsGross, format: .number)
}
HStack {
Text("Net:")
TextField("Net", value: $roundsdata.roundsNet, format: .number)
.textContentType(.name)
}
HStack {
Text("Rating:")
TextField("Rating", value: $roundsdata.roundsRating, format: .number)
.textContentType(.name)
}
HStack {
Text("Slope:")
TextField("Slope", value: $roundsdata.roundsSlope, format: .number)
.textContentType(.name)
}
}
}
}