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.