In my home view, I can see listName and date. Also, I can see productCount when I write elements.productCount, but I cannot see my productCount and listName when I go another view
HomeView
@StateObject var homeData = HomeViewModel()
// Fetching Data.....
@Environment(.managedObjectContext) var context
@FetchRequest(entity: ShoppingList.entity(), sortDescriptors: [NSSortDescriptor(key: "date", ascending: true)],animation: .spring()) var results : FetchedResults<ShoppingList>
var body: some View {
VStack{
NavigationView{
NavigationLink(destination: DetailView(list: homeData)){
ForEach(results){ element in
VStack(alignment: .leading) {
Text("(element.listName!)")
.font(.headline)
.foregroundColor(.black)
.padding(.top, 20)
DetailView
struct DetailView: View {
@ObservedObject var list = HomeViewModel()
var body: some View {
Text("(list.productCount)")
}
}
2
Answers
You should not create initiate
list
inDetailView
, because you pass it from home view – only declare it, so likeBtw, it looks strange that you put all
ForEach
into one NavigationLink (is it intended? that might bring you problems later)I think that you are doing some mistakes in your code (if your case is a
master - detail
app)Element
object (item form your fetched list)like that :
And your home view :