GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

SOLVED: Picker won't update

Forums > SwiftUI

@ketco  

Hey Folks,

I've got a problem with a picker. The picker shows all values correctly but it doesn't update the var chosenPlace when choosing an item.

import SwiftUI
import SwiftData

struct GameCreateView: View {
    @Environment(\.modelContext) private var modelContext
    @Environment(\.dismiss) var dismiss

    @Query private var places: [Place]
    @State private var chosenPlace: Place? = nil

    @State private var temperature: String = ""

    var body: some View {
        Form {
            Section {
                Picker("Platz", selection: $chosenPlace) {
                    Text("Platz wählen").tag(nil as Place?)
                    ForEach(places, id: \.self) { place in
                        HStack{
                            Text(String(place.zip) + " " + place.name)
                                .lineLimit(1)
                                .tag(place as Place?)

                            Spacer()
                        }
                        .frame(maxWidth: .infinity, alignment: .leading)
                        .lineLimit(1)
                    }
                }
                TextField("Temperatur", text: $temperature) 
            } 
        }
    }
}
@Model
final class Place {
    var name: String
    var zip: Int
    var type: String
    var courses: [String]

    init(name: String, zip: Int, type: String, courses: [String]) {
        self.name = name
        self.zip = zip
        self.type = type
        self.courses = courses
    }
}

Thanks for your help =) Ketco

   

@ketco  

Problem is resolved.

The .tag must be behind the HStack:

Picker("Platz", selection: $chosenPlace) {
                    Text("Platz wählen").tag(nil as Place?)
                    ForEach(places, id: \.self) { place in
                        HStack{
                            Text(String(place.zip) + " " + place.name)
                                .lineLimit(1)
                                .tag(place as Place?) // Verwende den Ort als Tag

                            Spacer()
                        }
                        .frame(maxWidth: .infinity, alignment: .leading)
                        .lineLimit(1)
                        .tag(place as Place?) // Verwende den Ort als Tag
                    }
                }

   

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until February 9th.

Click to save your free spot now

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

Reply to this topic…

You need to create an account or log in to reply.

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.