Post

Replies

Boosts

Views

Activity

Picker list based on a selection from another Picker list
I have come quite far with my first app and have various picker lists working well. However, I am stuck thinking through how to do this next picker. My data model has Golf Course name and Tee. For each golf course there are multiple Tees and not all the same across the Courses. Example of Data CourseA RedTee CourseA GreenTee CourseA BlueTee CouseB RedTee CourseB YellowTee CourseB WhiteTee I first give the client the ability to Pick a Course from a picker list. That works fine. Now I want to create a Picker list of Tees but only for that selected Course. So if the client selected CourseB they would be presented with these Tees to select from RedTee YellowTee WhiteTee How do I limit the second picker to only show the Tees for the selected Course? Then in an associated question, once i have the Course and the Tee, I want to Auto fill the rest of a form with Slope, Rating and Yardage. @Model class ScorecardData { var scorecardcourseName: String var scorecardTee: String var scorecardSlope: Double var scorecardRating: Double var scorecardYardage: Int Here is my code for the Course Picker List Picker("", selection: $selectedCourse) { Text("Select a Course").tag(" ") ForEach(courses, id: \.self) { course in Text(course.courseName) .tag(course.courseName) } } .onChange(of: selectedCourse) { if(selectedCourse != nil) { roundsdata.roundscourseName = selectedCourse! } } Here is my current Picker list but its pulling all Tees for all Courses Picker("", selection: $selectedTee) { Text("Select Tee").tag(" ") ForEach (tees, id: \.self) { tee in Text(tee.scorecardTee) .tag(tee.scorecardTee) } } .onChange(of: selectedTee) { if(selectedTee != nil) { roundsdata.roundsTee = selectedTee! } } My @State and @Query statements are as follows in case there is a change there that is needed as well, @State private var selectedTee: String? = "Select Tee" @Query(sort: \ScorecardData.scorecardcourseName) private var tees: [ScorecardData] @State private var selectedCourse: String? = "Select Course" @Query(sort: \CourseData.courseName) private var courses: [CourseData]
3
0
233
Dec ’24
NavigationStack Fatal error: ‘try!
I am struggling to get pickers work in a form. I add NavigationStack and get this error Fatal error: 'try!' expression unexpectedly raised an error: SwiftUI.AnyNavigationPath.Error.comparisonTypeMismatch I have tried most every format or picker setup as well as DatePicker and can’t seem to determine how to get the selected item in a picker list saved. running: Xcode 16.1 import SwiftUI import SwiftData struct RoundsEditView: View { @Bindable var roundsdata: RoundsData @Environment(.modelContext) private var modelContext // @Environment(.dismiss) var dismiss @State private var playDate = Date.now @State private var selectedTee = "Gold" let tees = ["Black", "Blue", "White", "Gold", "Red", "Silver"] @Query(sort: \PlayerData.playerHandle) private var players: [PlayerData] @State private var selectedHandle: PlayerData? = nil var body: some View { NavigationStack { Form { HStack { Text("Course:") TextField("Course Name", text: $roundsdata.roundscourseName) .textContentType(.name) } HStack { // DatePicker("Date:", selection: $playDate, in: ...Date(), DatePicker("Date:", selection: $playDate, displayedComponents: [.date]) // DatePicker("Date:", selection: $playDate, in: ...Date(), // displayedComponents: .date).onChange(of: playDate) { oldState, newState in model.youDidChangeMethod(from: oldState, to: newState) } 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 } // HStack { // Text("Tee:") // TextField("Tee", text: $roundsdata.roundsTee) // .textContentType(.name) // } HStack { Picker("Tee:", selection: $selectedTee) { ForEach(tees, id: \.self) { Text($0) } } } 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) } } .navigationTitle("Edit Rounds") } } }
10
0
514
Nov ’24
Picker and DatePicker selection confusion
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) } } } }
2
0
254
Nov ’24