'NavigationLink in SwiftUI bug
I have a big problem and I spend hours to find a solution. I will try to be as descriptive as I can be, because there is a lot of code involved and can not post here all. So, to explain my issue, I have a view which have a toolbarItem in the top right corner, a "Cart". After you press on the Cart, there will be 2 or 3 screens where u need to complete information about an order. At the finall step, after i sent the order, "WaitingOrderView" screen will pop up, but there is a bug
So, after the final step, my screen goes crazy. The screen changes from main view to WaitingOrderView without stopping. The application stops responding, and in order to recover, it must be closed and reopened.
struct MainView: View {
@EnvironmentObject var syncViewModel : SyncViewModel
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(sortDescriptors: [])
private var menus : FetchedResults<LocalMenu>
var body: some View {
TabView{
NavigationView{
MeniuriView()
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
ToolbarButtons(numberOfProducts: menus.count)
}
ToolbarItem(placement: .navigationBarLeading) {
Text(Texts.mainViewText1)
.font(.system(size: 24))
.fontWeight(.bold)
.padding()
}
}
}
.tabItem {
Text(Texts.mainViewText2)
Image(systemName: "fork.knife")
}
NavigationView {
AlteleView()
}
.tabItem {
Text(Texts.mainViewText3)
Image("altele", bundle: Bundle.main)
}
}
.accentColor(Color.tabItemColor)
}
struct ToolbarButtons: View {
@EnvironmentObject var syncViewModel : SyncViewModel
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(sortDescriptors: [])
private var cartOrder : FetchedResults<CartOrders>
@State var pushActive : Bool = false
var numberOfProducts : Int
var body: some View {
ZStack {
Spacer()
if syncViewModel._order.status == 0 && syncViewModel._order.id == 0{
NavigationLink(destination: CartView()
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Text(Texts.cartText)
.font(.system(size: 24))
.bold()
}
}
, label: {
Image("otherIcon")
.padding(.top, 5)
}
)
}
else if syncViewModel._order.id == cartOrder.first?.id ?? Int32(syncViewModel._order.id) {
NavigationLink(isActive: $pushActive) {
WaitingOrderView()
} label: {
ZStack(alignment: .topTrailing) {
Image("cartIcon")
.padding(.top, 5)
if numberOfProducts > 0 {
Text("\(numberOfProducts)")
.font(.caption2).bold()
.foregroundColor(.white)
.frame(width: 15, height: 15)
.background(Color.tabItemColor)
.cornerRadius(50)
.offset(x: 10, y: -8)
}
}
}
.onAppear {
pushActive = true
}
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

