简体   繁体   中英

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 ?? "" 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 (eg @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 ?? ""
                                }
                        }
                    }
                }
...

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)
                        }
                    }
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM