如何使用选择器中的核心数据值? #SwiftUI #CoreData

How can I use Core Data value from picker? #SwiftUI #CoreData

struct PickerView: View {

var items0 : [PageName]
@State var selectedPage: PageName?

init(items0: [PageName]) {
    self.items0 = items0
    self._selectedPage = State(initialValue: items0.first)
}

var body: some View {
    Picker(selection: $selectedPage, label: Text("Page")) {
        ForEach(items0) { item in
            Text(item.pageName ?? "").tag(item as PageName?)
        }
    }
    Text("\((selectedPage?.pageName)!)")
}







NavigationView {
                Form {
                    PickerView(items0: Array(items0))
                }
                .foregroundColor(.blue)
                .background(Color.yellow)
            }

我可以在 Picker 中成功选取 Core Data 的值,但我不知道如何在我的主视图中使用这个值...

很多事情我已经尝试过了,但核心数据让我发疯。

有人可以帮助我在 SwiftUI 中将选取器的选取值与 Core Data 一起使用吗?

我误解了你的问题,我的原始评论见下文。评论里有很多信息。

import SwiftUI
struct ParentPickerView: View {
    @Environment(\.managedObjectContext) private var viewContext
    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \PageName.pageName, ascending: true)],
        animation: .default)
    private var items0: FetchedResults<PageName>
    //State is a source of truth
    @State var selectedPage: PageName? = nil
    var body: some View {
        NavigationView {
            Form {
                Text(selectedPage?.pageName ?? "not selected")
                PickerView(items0: Array(items0), selectedPage: $selectedPage)
            }
            .foregroundColor(.blue)
            .background(Color.yellow)
        }
    }
}
struct PickerView: View {
    //If you don't need all your PageName in your parentView it would be best to have the fetch here
    //    @Environment(\.managedObjectContext) private var viewContext
    //    @FetchRequest(
    //        sortDescriptors: [NSSortDescriptor(keyPath: \PageName.pageName, ascending: true)],
    //        animation: .default)
    //    private var items0: FetchedResults<PageName>
    var items0: [PageName]
    //Binding is a two-way connection
    @Binding var selectedPage: PageName?
    //You don't need custom init in SwiftUI because they are struct
    //    init(items0: [PageName], selectedPage: Binding<PageName?>) {
    //        self.items0 = items0
    //        self._selectedPage = selectedPage
    //
    //        //@State Init here is bad practice as you are experiencing it becomes a dead end
    //        //self._selectedPage = State(initialValue: items0.first)
    //    }
    var body: some View {
        Picker(selection: $selectedPage, label: Text("Page")) {
            ForEach(items0) { item in
                Text(item.pageName ?? "").tag(item as PageName?)
            }
        }
        Text("\((selectedPage?.pageName ?? "not selected"))")
            
            .onAppear(){
                //Set your initial item here only if the selected item is nil
                if selectedPage == nil{
                    selectedPage = items0.first
                }
            }
    }
}