Arkyyn
Arkyyn

Reputation: 51

SwiftUI Setting Initial Picker Value From CoreData

I am pulling my hair out trying to figure out how to get my picker to show the already stored CoreData value. I want it to show on the right side of the picker as if the user just selected it. I have tried adding self.updatedItemAttribute = self.editItem.attribute ?? "" prior to the picker to set the initial value but that does not build. I also tried defining it in @State (e.g. @State var updatedItemAttribute: String = self.editItem.attribute) but that does not build either. If I add a TextField prior to the picker it will set the value, but I do not want to have a TextField with the value just to get it to set. Any ideas on how I get updatedItemAttribute set to editItem.attribute just prior to the picker? Thanks.

import CoreData
import SwiftUI

struct EditItemView: View {
    @Environment(\.managedObjectContext) var moc
    @Environment(\.presentationMode) var presentationMode
    @ObservedObject var editItem: Item

    @State var updatedItemName: String = ""
    @State var updatedItemAttribute: String = ""

    let attributes = ["Red", "Purple", "Yellow", "Gold"]

    var body: some View {
        NavigationView {
            Form {
                Section {
                    TextField("Name of item", text: $updatedItemName)
                        .onAppear {
                            self.updatedItemName = self.editItem.name ?? ""
                    }
                    Picker("Test attribute", selection: self.$updatedItemAttribute) {
                        ForEach(attributes, id: \.self) {
                            Text($0)
                                .onAppear {
                                    self.updatedItemAttribute = self.editItem.attribute ?? ""
                                }
                        }
                    }
                }
...

Upvotes: 2

Views: 3105

Answers (1)

Asperi
Asperi

Reputation: 257711

You have to this in init as shown below

struct EditItemView: View {
    @Environment(\.managedObjectContext) var moc
    @Environment(\.presentationMode) var presentationMode
    @ObservedObject var editItem: Item

    @State private var updatedItemName: String
    @State private var updatedItemAttribute: String

    init(editItem item: Item) {             // << updated here
        self.editItem = item
        self._updatedItemName = State<String>(initialValue: item.name ?? "")
        self._updatedItemAttribute = State<String>(initialValue: item.attribute ?? "")
    }

    let attributes = ["Red", "Purple", "Yellow", "Gold"]

    var body: some View {
        NavigationView {
            Form {
                Section {
                    TextField("Name of item", text: $updatedItemName)
                    Picker("Test attribute", selection: self.$updatedItemAttribute) {
                        ForEach(attributes, id: \.self) {
                            Text($0)
                        }
                    }
                }
            }
        }
    }
}

Upvotes: 6

Related Questions