Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SwiftUI: Doesn't pass data from selected list item into detail view

I have been trying to show selected item’s detail view but can’t seem to get it working. When trying to get the name of the item into the detail view by diaries.name I get an error Value of type '[DiaryItem]' has no member 'name'

ForEach(diaries.items) {item in
NavigationLink(destination: DiaryDetailView(diaries: diaries)) {
     HStack {
     VStack(alignment: .leading) {
     Text(item.name)
    .font(.headline)
    }
               Spacer()
                        
  } //: HSTACK
 }//: FOR
}

And here is the detail view

struct DiaryDetailView: View {
    
    let diaries: Diaries
    
    var body: some View {
        VStack {
            Text(diary.name)
        }
}
}
struct DiaryDetailView_Previews: PreviewProvider {
    static var previews: some View {
        DiaryDetailView(diaries: Diaries())
    }
}

And my class where it saves the form information

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Diaries: ObservableObject {
    @Published var items = [DiaryItem]() {
        didSet {
            if let encoded = try? JSONEncoder().encode(items) {
                UserDefaults.standard.set(encoded, forKey: "Items")
            }
        }
    }

    init() {
        if let savedItems = UserDefaults.standard.data(forKey: "Items") {
            if let decodedItems = try? JSONDecoder().decode([DiaryItem].self, from: savedItems) {
                items = decodedItems
                return
            }
        }

        items = []
    }
}
struct DiaryItem: Identifiable, Codable {
    var id = UUID()
    let name: String
    let fishName: String
    let info: String
}

>Solution :

you probably wanted something like this:

ForEach(diaries.items) {item in
NavigationLink(destination: DiaryDetailView(diary: item)) { // <-- here
     HStack {
     VStack(alignment: .leading) {
     Text(item.name)
    .font(.headline)
    }
               Spacer()
                        
  } //: HSTACK
 }//: FOR
}

struct DiaryDetailView: View {
    let diary: DiaryItem  // <-- here
    
    var body: some View {
        VStack {
            Text(diary.name)
        }
}
}

and

struct DiaryDetailView_Previews: PreviewProvider {
    static var previews: some View {
        DiaryDetailView(diary: DiaryItem(name: "name1", fishName: "fish1", info: "info1"))
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading