Strange behavior when changing view

Good morning, could anyone help me solve this behavior when changing view? I have a View used as sidebar with different NavigationLinks, one to create a Record, one to create an Account, one to show all records, and the last one to show specific account’s records.

NavigationView {
            VStack(alignment: .leading) {
                Group {
                    Text("Records")
                        .font(.system(size: 18))
                        .fontWeight(.bold)
                        .padding(.top, 5)
                    NavigationLink(destination: AddRecord(auxiliaryAccounts: $auxiliaryAccounts, auxiliaryArray: $auxiliaryArray, exercise: $exercise)) {
                        Label("Add Record", systemImage: "plus.app")
                    }
                }
                Group {
                    Text("Accounts")
                        .font(.system(size: 18))
                        .fontWeight(.bold)
                        .padding(.top, 5)
                    NavigationLink(destination: AddAuxAccount(auxiliaryArray: $auxiliaryArray, exercise: $exercise, secondaryAccount: $secondaryAccounts)) {
                        Label("New account", systemImage: "plus.app")
                    }
                }
                Divider()
                Group {
                    Text("Ledger")
                        .font(.system(size: 18))
                        .fontWeight(.bold)
                    NavigationLink(destination: TableView(tableData: exercise.entries, accountName: "General Ledger", accountNumber: 0)) {
                        Label("General Ledger", systemImage: "chart.line.uptrend.xyaxis")
                    }
                }
                Divider()
                Group {
                    Text("Accounts")
                        .font(.system(size: 18))
                        .fontWeight(.bold)
                    List(filteredAccounts, id: \.id!, selection: $selectedAccount) { account in
                        NavigationLink(destination: TableView(tableData: account.entry, accountName: account.name, accountNumber: account.number), label: {
                            VStack(alignment: .leading) {
                                Text("\(account.number)")
                                Text(account.name)
                            }.padding(10)
                        })
                        Divider()
                    }.searchable(text: $searchQuery, prompt: "Search for account")
                        .onChange(of: searchQuery) { _ in
                          filterAccounts()
                        }
                        .onSubmit(of: .search) {
                        filterAccounts()
                      }
                }
            }
            .onAppear {
                loadAccounts()
                filterAccounts()
            }
            .listStyle(SidebarListStyle())
            .frame(minWidth: 250, idealWidth: 250, maxWidth: 300)

            TableView(tableData: exercise.entries, accountName: "General Ledger", accountNumber: 0)
        }

The problem resides when I switch view. I can navigate perfectly from “Accounts List” view (any of them) to one of the other three, but if I switch between “Add record” and “Add account”, nothing happens, the current view remain active, but as soon as I select again an “Accounts List” view, the app first open the selected one (correctly) and then it switch to the previous selected (Add Record or Add Account), as if it save the selection somewhere and it execute it only if I select a view from the list. All destination views are made the same way, starting with a VStack, so I think the problem resides in my Sidebar.
Thank you very much.

Hi,

NavigationLink does not work well in macOS, at least not in the current version.

I recommend having the items in your sidebar set a property or properties and have a detail view that uses these to decide what to show.

Right now, your detail view is preset to show a TableView so that will always be the default. If you change this to a view that makes its own decisions about what to show: TableView, AddRecord, AddAuxAccount or whatever, I think you will have much more success.

Sarah

1 Like

In MacOS 13 beta 5 and Xcode 14 beta 5, following is logged during this strange behaviour:

SwiftUI encountered an issue when pushing a NavigationLink. Please file a bug.

But it works as expected after adopting to NavigationSplitView

NavigationSplitView {

} detail: {
    TableView(tableData: exercise.entries, accountName: "General Ledger", accountNumber: 0)
}

Apple has changed navigation completely in the new system. You will need to use NavigationSplitView instead of NavigationView. In my tests so far, this seems to work really well.