SwiftUI - Navigate back when list that I came from refresh

Viewed 28

I have a list of elements in SwiftUI. And I can navigate to a detail view.

But sometimes I refresh data from a server and that list can be refreshed.

The problem is that if I'm in a detail view, and here my list is refreshed in background, and then, automagically, the app makes a back navigation.

I understand that this is because the list is remaked and in some way the reference is lost, but there are some way I can prevent this back navigation?

In my home screen I have a NavigationView, a HStack for the list, and inside a NavigationLink. Here is how is structurated:

NavigationView {
    HStack {
        ForEach(items) { item in 
            NavigationLink(destination: DetailView(id: item.id)) {
                Text(item.name)
            }
        }
    }

}

UPDATE:

Finally, I fixed, but not in the way that I want :(

Remove NavigationLink from ForEach and put outside.

//This is outside body
@State private var isShowingDetailView = false
@State private var selectedID: String = ""


NavigationView {
    HStack {
        NavigationLink(destination: DetailView(id: selectedID), isActive: $isShowingDetailView) {  }

        ForEach(items) { item in 
            Text(item.name)
                .onTapGesture {
                    self.selectedID = item.id
                    self.isShowingDetailView = true
                }
        }
    }

}
0 Answers
Related