SwifUI activates all available indices in ForEach

后端 未结 1 926
难免孤独
难免孤独 2021-01-21 05:45

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

相关标签:
1条回答
  • 2021-01-21 06:22

    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.

    0 讨论(0)
提交回复
热议问题