Deselect list row in SwiftUI

Hello,

I am creating a macOS app with SwiftUI and I added a sidebar with NavigationView and inside I added a List with NavigationLinks. I also added a separate NavigationLink at the bottom of the Window.

The behavior I want to have is, when I press the NavigationLink at the bottom, then to deselect all the other NavigationLinks in the sidebar List.

Most probably I still haven't properly understood SwiftUI and it's declarative paradigm yet, and I can't see a way to do this.

Could someone help me?

This is the code I have written:

Enum
Code Block
enum Menu: String, CaseIterable {
    case library = "Library"
    case catalogue = "Catalogue"
    case filter = "Filter"
  }

Main Code
Code Block
struct SplitView: View {
    var body: some View {
        NavigationView {
            VStack(alignment: .leading, spacing: nil) {
                MenuList()
                Divider()
                Spacer()
                NavigationLink(destination: FilterSpace()) {
                    HStack {
                        Text("􀌈")
                        Text("Filter")
                    }
                        .padding(5.0)
                        .overlay(RoundedRectangle(cornerRadius: 10).stroke(Color.gray, lineWidth: 1))
                }
                .cornerRadius(10)
                .padding([.leading, .trailing], 7.0)
                .buttonStyle(BorderlessButtonStyle())
                Spacer()
            }
        }
.navigationViewStyle(DoubleColumnNavigationViewStyle())
.frame(minWidth: 800, maxWidth: .infinity, minHeight: 500, maxHeight: .infinity)
    }
}

Sidebar List
Code Block
struct MenuList: View {
    @State private var selection: Menu? = .library
    var body: some View {
        List(selection: $selection) {
            Section(header: Text("Main Menu")) {
                NavigationLink(destination: LibrarySpace()) {
                    Text(Menu.library.rawValue)
                        .font(.subheadline)
                        .padding(5)
                }
                NavigationLink(destination: CatalogueSpace()) {
                    Text(Menu.catalogue.rawValue)
                        .font(.subheadline)
                        .padding(5)
                }
            }
        }
        .listStyle(SidebarListStyle())
        .frame(minWidth: 150, maxWidth: 150)
        .onAppear {
                self.selection = .catalogue
}
    }
}


Answered by Claude31 in 639989022
May have a look here on how to select an item.
https://stackoverflow.com/questions/60139184/swiftui-navigationlink-macos-default-selected-state

Similar principle should allow to deselect all as well.
Accepted Answer
May have a look here on how to select an item.
https://stackoverflow.com/questions/60139184/swiftui-navigationlink-macos-default-selected-state

Similar principle should allow to deselect all as well.
Thanks, I saw the code in the link and I adjusted it to my requirements and with a bit of trial and error, it worked.
Could you please show your code? I'm getting used to SwiftUI's declarative paradigm too so it's a little bit hard to adapt the code in the link from MacOS to iOS. Thanks in advance.

Could you please show your code? I'm getting used to SwiftUI's declarative paradigm too so it's a little bit hard to adapt the code in the link from MacOS to iOS. Thanks in advance.

Hello,

I just removed the NavigationLinks, created the list options with a ForEach statement and the bottom one separately and called the views directly with if else statements, as I couldn't use a switch. I also wrapped the selected variable as a @state and created another one wrapped as @Binding, then I separated my views to make it more clear, readable and modular.

That was all it needed to make it work!
Deselect list row in SwiftUI
 
 
Q