首页 > 解决方案 > 如何通过带有 FetchedResults 和 CoreData 的导航链接传递数据?

问题描述

我正在尝试通过 ScrollView 中的导航链接将 FetchResult 数据从一个屏幕传递到另一个屏幕。

当我尝试在 DetailView 屏幕中调用传递的数据时,出现以下错误: Instance member "" cannoth be used on type "SavedPoem"

下面的代码 - 保存的诗歌列表

import SwiftUI

struct SavedPoemList: View {
    
    @Environment(\.managedObjectContext) var moc
    @FetchRequest(entity: SavedPoem.entity(), sortDescriptors: []) var savedpoems : FetchedResults<SavedPoem>
    
    var body: some View {
        
        VStack (alignment: .leading, spacing: 0) {
            
            HStack{
                Text("Your Saved Poems")
                    .font(.title)
                    .fontWeight(.black)
                    .foregroundColor(.black)

                    
                Spacer()
                    
                    
            }.padding(.bottom)
            .padding(.trailing)
            .padding(.leading)
               
            ScrollView {
                    
                ForEach(savedpoems, id:\.title) {SavedPoem in
                   
                    NavigationLink (destination: DetailViewSaved()){
                      
                        ZStack {
                            
                            Rectangle()
                                .fill(Color.white)
                                .frame(width: UIScreen.screenWidth - 40, height: 70)
                                .cornerRadius(5)
                                .padding([.horizontal], 20)
//                                .shadow(color: .gray, radius: 10)
                                                        
                            HStack {
                                VStack (alignment: .leading){
                                    Text("\(SavedPoem.title ?? "")")
                                        .font(.headline)
                                        .foregroundColor(.black)
                                        .lineLimit(1)
                                        .padding(.bottom, 3)
                                        
                                    
                                    Text("\(SavedPoem.author ?? "")")
                                        .font(.subheadline)
                                        .foregroundColor(.secondary)
                                }
                                .padding(.trailing)
                                Spacer()
                                
                            }
                            .padding()
                        }.padding(.bottom,10)
                        
                    }
                    
                }.onDelete(perform: self.remove)
                
            }
            .navigationTitle("My Saved Poems")
            .navigationBarHidden(true)
            .edgesIgnoringSafeArea(.top)
            .padding(.bottom, 30)
            
                    

            }
        .padding(.horizontal, 30)
        .edgesIgnoringSafeArea(.bottom)
        
    }
        func remove(at offsets : IndexSet) {
            for index in offsets {
                let delete = savedpoems[index]
                self.moc.delete(delete)
            }
            try? self.moc.save()
        }
}

和详细视图的代码

import SwiftUI

struct DetailViewSaved: View {
    
    @ObservedObject var fetch = FetchPoem()
    
    @Environment(\.managedObjectContext) var moc
    @FetchRequest(entity: SavedPoem.entity(), sortDescriptors: []) var savedpoems : FetchedResults<SavedPoem>
    
    @State private var saved : Bool = false
    
    var currentDate = Text(Date().addingTimeInterval(600), style: .date)
    
    var body: some View {
            
            VStack {
                
                HStack{
                    
//                    NavigationLink( destination: HomeView())
//                    {
//
//                        Image(systemName: "arrow.backward")
//                            .font(.system(size: 25, weight: .heavy))
//                            .foregroundColor(.black)
//                    }
                    
                    
                    Spacer(minLength: 0)
                    
                    Button(action:
                            {
//                            self.moc.delete(delete)
                            
                            try? self.moc.save()
     
                    }) {
                        
                        Image(systemName: saved ? "bookmark.fill": "bookmark")
                            .font(.system(size: 25, weight: .heavy))
                            .foregroundColor(.black)
                    }
                } 
                
                ScrollView {
                    
                    VStack {
                        
                        HStack{
                            
                            VStack (alignment: .leading) {
                                
                                Text("Today's Poem, \(currentDate)")
                                    .font(.subheadline)
                                    .foregroundColor(Color.gray)
                                    .padding(.bottom, 20)
                                    .padding(.top, 10)
                                    
                                    
                                    Text("\(SavedPoem.title ?? "")")
//                                        .font(.largeTitle)
//                                        .fontWeight(.heavy)
//                                        .foregroundColor(.black)
//                                        .padding(.bottom, 20)
//                                        .lineSpacing(0)
                                    
//                                    Text("BY "+poem.author.uppercased())
                                    Text("BY \(SavedPoem.author ?? "")")
//                                        .font(.subheadline)
//                                        .foregroundColor(Color.gray)
//                                        .padding(.bottom, 20)
                                    
                                    HStack {
                                        Text("\(SavedPoem.lines ?? "")")
//                                            .font(.body)
//                                            .foregroundColor(.black)
//                                            .padding(.bottom)
//                                            .lineSpacing(5)
//
                                        Spacer()
                                    }
                                    
                                    
               
                                    Spacer()
                                }
                            .padding()
                            }
                            
                        }
                        
                    }
                }
                
            }

        }
        

我错过了什么吗?如何解决此问题,并将列表视图中 Fetchedresults 中的数据传递到详细屏幕?

提前致谢。

编辑回复代码

已保存的诗歌列表:

import SwiftUI

struct SavedPoemList: View {
    
    @Environment(\.managedObjectContext) var moc
    @FetchRequest(entity: SavedPoem.entity(), sortDescriptors: []) var savedpoems : FetchedResults<SavedPoem>
    
    var body: some View {
        
        VStack (alignment: .leading, spacing: 0) {
            
            HStack{
                Text("Your Saved Poems")
                    .font(.title)
                    .fontWeight(.black)
                    .foregroundColor(.black)

                    
                Spacer()
                
                Text("Edit")
                    .font(.subheadline)
                    .fontWeight(.black)
                    .foregroundColor(.gray)
                    
                    
            }.padding(.bottom)
            .padding(.trailing)
            .padding(.leading)
               
            ScrollView {
                    
                ForEach(savedpoems, id:\.title) {SavedPoem in
                   
                    NavigationLink (destination: DetailViewSaved(savedPoem: SavedPoem)){
                      
                        ZStack {
                            
                            Rectangle()
                                .fill(Color.white)
                                .frame(width: UIScreen.screenWidth - 40, height: 70)
                                .cornerRadius(5)
                                .padding([.horizontal], 20)
//                                .shadow(color: .gray, radius: 10)
                                                        
                            HStack {
                                VStack (alignment: .leading){
                                    Text("\(SavedPoem.title ?? "")")
                                        .font(.headline)
                                        .foregroundColor(.black)
                                        .lineLimit(1)
                                        .padding(.bottom, 3)
                                        
                                    
                                    Text("\(SavedPoem.author ?? "")")
                                        .font(.subheadline)
                                        .foregroundColor(.secondary)
                                }
                                .padding(.trailing)
                                Spacer()
                                
                            }
                            .padding()
                        }.padding(.bottom,10)
                        
                    }
                    
                }.onDelete(perform: self.remove)
                
            }
            .navigationTitle("My Saved Poems")
            .navigationBarHidden(true)
            .edgesIgnoringSafeArea(.top)
            .padding(.bottom, 30)
            
                    

            }
        .padding(.horizontal, 30)
        .edgesIgnoringSafeArea(.bottom)
        
    }
        func remove(at offsets : IndexSet) {
            for index in offsets {
                let delete = savedpoems[index]
                self.moc.delete(delete)
            }
            try? self.moc.save()
        }
}

详细视图

import SwiftUI

struct DetailViewSaved: View {
    
    @ObservedObject var fetch = FetchPoem()
    
    @Environment(\.managedObjectContext) var moc
    @FetchRequest(entity: SavedPoem.entity(), sortDescriptors: []) var savedpoems : FetchedResults<SavedPoem>
    
    @State private var saved : Bool = false
    
    var savedPoem : SavedPoem
    
    var currentDate = Text(Date().addingTimeInterval(600), style: .date)
    
    var body: some View {
            
            VStack {
                
                HStack{
                    
//                    NavigationLink( destination: HomeView())
//                    {
//
//                        Image(systemName: "arrow.backward")
//                            .font(.system(size: 25, weight: .heavy))
//                            .foregroundColor(.black)
//                    }
                    
                    
                    Spacer(minLength: 0)
                    
                    Button(action:
                            {
//                            self.moc.delete(delete)
                            
                            try? self.moc.save()
     
                    }) {
                        
                        Image(systemName: saved ? "bookmark.fill": "bookmark")
                            .font(.system(size: 25, weight: .heavy))
                            .foregroundColor(.black)
                    }
                } 
                
                ScrollView {
                    
                    VStack {
                        
                        HStack{
                            
                            VStack (alignment: .leading) {
                                
//                                Text("Today's Poem, \(currentDate)")
//                                    .font(.subheadline)
//                                    .foregroundColor(Color.gray)
//                                    .padding(.bottom, 20)
//                                    .padding(.top, 10)
                                    
                                    
                                    Text("\(savedPoem.title ?? "")")
//                                        .font(.largeTitle)
//                                        .fontWeight(.heavy)
//                                        .foregroundColor(.black)
//                                        .padding(.bottom, 20)
//                                        .lineSpacing(0)
                                    
//                                    Text("BY "+poem.author.uppercased())
//                                    Text("BY \(SavedPoem.author ?? "")")
//                                        .font(.subheadline)
//                                        .foregroundColor(Color.gray)
//                                        .padding(.bottom, 20)
                                    
//                                    HStack {
//                                        Text("\(SavedPoem.lines ?? "")")
//                                            .font(.body)
//                                            .foregroundColor(.black)
//                                            .padding(.bottom)
//                                            .lineSpacing(5)
//
                                        Spacer()
                                    }
                                    
                                    
               
                                    Spacer()
                                }
                            .padding()
                            }
                            
                        }
                        
                    }
                }
                
            }

        }

   struct DetailViewSaved_Previews: PreviewProvider {
        static var previews: some View {
            DetailViewSaved(savedPoem: SavedPoem)
        }
    }
    
        

标签: swiftcore-dataswiftui

解决方案


我用你的例子做了一个例子:

SavedPoemList:将一个项目从您的数组传递给您Detail View,就像这样:

NavigationLink (destination: DetailViewSaved(savedPoem: SavedPoem)){
            VStack{
                Text("Your row view")
            }
}

并在您的Detail View

struct DetailViewSaved: View {
    var savedPoem : SavedPoem
    var body: some View {
        Text("Your view")
    }
}

推荐阅读