I have a problem programmatically opening and closing a View in SwiftUI:
With the code below SwiftUI opens each index of contactsArray one after another, when clicking on
You join all NavigationLink/s to one state (one-to-many), so, no surprise, when you toggle this state all links are activated.
Instead you need something like the following
@State private var selectedContact: String? = nil // or put it elsewhere
...
NavigationLink(destination: ContactsDetailsView(contact: contact),
tag: contact.id, selection: $selectedContact) {
Text(contact.surname).bold() + Text(", ") + Text(contact.forename)
}
, where selectedContact
is id of contact link to be activated. Then all you need is to decide which contact id to assign to selectedContact
.