Hello everyone. I am developing an application with SwiftUI. I am having trouble with NavigationStack(path: )
.
1st problem:
After the application runs, after clicking on the first list item, there is a flicker in the title section. I think it is the .navigationDestination that causes this problem, because when I change the navigationLink to Button in the “ActiveRegQueryView” screen, this problem disappears.
2nd Problem:
When you click on a list item, sometimes it stays pressed (grayed out) and does not take you to the screen (Video 1). If you try to click on an item more than once, navigatinLink passes more than one value to path and opens more than one page (I noticed this with path.count) (Video 2). I don't have this problem if you edit the back button on the screen it takes you to (ActiveRegDetailView). (vm.path.removeLast()) The reason I use path is to close multiple screens and return to the start screen.
Video 1:
Video 2:
Main View:
import SwiftUI
struct ActiveRegView: View {
@Environment(NavigationViewModel.self) private var navViewModel
@AppStorage("sortOption") private var sortOrder: sorting = .byBrand
@State private var searchText = ""
var body: some View {
@Bindable var navViewModel = navViewModel
NavigationStack(path: $navViewModel.path) { // <- if i don't use path everything is OK
List {
ActiveRegQueryView(searchText: searchText, sortOrder: sortOrder) // <- Dynamic Query View
}
.navigationDestination(for: Registration.self, destination: {
ActiveRegDetailView(reg: $0)
.toolbar(.hidden, for: .tabBar)
})
}
}
}
Dynamic Query View:
import SwiftData
import SwiftUI
struct ActiveRegQueryView: View {
@Query private var regs: [Registration]
@Environment(NavigationViewModel.self) var vm
init(searchText: String, sortOrder: sorting) {
var order: SortDescriptor<Registration>
switch sortOrder {
case .byBrand:
order = SortDescriptor(\.brand)
case .byDateDescending:
order = SortDescriptor(\.entryRegistration.entryDate, order: .reverse)
case .byDateAscending:
order = SortDescriptor(\.entryRegistration.entryDate)
}
_regs = Query(filter: #Predicate {
if !searchText.isEmpty {
if $0.activeRegistration && ($0.brand.localizedStandardContains(searchText) || $0.model.localizedStandardContains(searchText) || $0.plate.localizedStandardContains(searchText)) {
return true
} else {
return false
}
} else {
return $0.activeRegistration
}
}, sort: [order])
}
var body: some View {
ForEach(regs) { reg in
NavigationLink(value: reg) {
ListRowView(reg: reg)
}
// Button {
// vm.path.append(reg)
// } label: {
// ListRowView(reg: reg)
// }
// .buttonStyle(.plain)
}
}
}
I look forward to your ideas for solutions. Thank you for your time.