ForEach NavigationLink with State and Binding

gurehbgui

I'm trying to generate a ForEach with a NavigationLink and use State and Binding to pass some entity around:

struct ContentView: View {
    @Environment(\.managedObjectContext) var moc

    @FetchRequest(
        entity: MyEntity.entity(),
        sortDescriptors: [
            NSSortDescriptor(keyPath: \MyEntity.name, ascending: true)
        ]

    ) var entries: FetchedResults<MyEntity>

    var body: some View {
        NavigationView {
            List {
                ForEach(entries, id: \.self) { (entry: MyEntity) in
                    NavigationLink(destination: DetailView(myEntry: $entry)) {
                        Text(entry.name!)
                    }
                }.
            }
        }
    }
}

And then the following view:

struct DetailView: View {
    @Binding var myEntry: MyEntity

    var body: some View {
        Text(myEntry.name!)
    }
}

The problem is I can not pass the value to Detail view since the error:

Use of unresolved identifier '$entry'

What is wrong here and how to solve this?

If I just have a simple @State its no problem to pass it via the binding, but I want/need to use it in the ForEach for the FetchedResults

EDIT: If I remove the $ I get Cannot convert value of type 'MyEntity' to expected argument type 'Binding<MyEntity>'

EDIT2: The purpose is to pass some object to DetailView and then pass it back later to ContentView

Asperi

Use the following in ForEach

   ForEach(entries, id: \.self) { entry in
        NavigationLink(destination: DetailView(myEntry: entry)) {
            Text(entry.name!)
        }
    }

and the following in DetailView

struct DetailView: View {
    var myEntry: MyEntity

    var body: some View {
        Text(myEntry.name!)
    }
}

The MyEntity is class, so you pass object by reference and changing it in DetailView you change same object.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Custom Binding of State inside ForEach

NavigationLink back not working with @Binding

Problem with ForEach and NavigationLink in SwiftUI

NavigationLink problem when using it in a ForEach

SwiftUI: NavigationLink pops immediately if used within ForEach

Unable to activate NavigationLink programmatically, in a List ForEach

Passing item using ForEach into DetailView through NavigationLink

Swiftui navigationLink macOS default/selected state

comment foreach binding vs foreach binding in knockoutjs

@Binding and ForEach in SwiftUI

Binding in a ForEach in SwiftUI

Foreach binding object with array

Is the foreach binding syntax valid

knockoutjs: foreach binding with filter

SwiftUI @State vs Binding

Observing Binding or State variables

Optional State or Binding in SwiftUI

State of variable not binding

Input field not binding with the state

SwiftUI - TabView/NavigationLink navigation breaks when using a custom binding

Bug with NavigationLink and @Binding properties causing unintended Interactions in the app

SwiftUI - List / ForEach in combination with NavigationLink and isActive doesn't work properly

How can implement a navigationLink inside of a LazyVGrid and ForEach statement?

NavigationLink when destination's required parameter is from optional state variable

State Variables and ForEach Swift

dynamic binding in SwiftUI ForEach for a field

Binding scope to item in forEach loop

Equivalent for optionsAfterRender on a foreach binding in knockout

data binding with knockout foreach is not working