Hi, I experienced this horrible problem and in my case was when using TabView.
I have TabView in my ContentView and then one of the List views inside had a NavigationView
At the beginning I thought it was something wrong with my code cos was working previously. But after struggling a while. I moved the NavigationView to wrap the whole TabView and that fixed the issue.
So I will write here both examples the Non working one and the fix I did in case that might help others.
Problematic Code
struct ContentView: View {
@EnvironmentObject var dataModel: DataModel
var body: some View {
TabView {
LibraryList()
.tabItem {
Label(L10n.library, systemImage: Images.System.library)
}
SettingsList()
.tabItem {
Label(L10n.more, systemImage: Images.System.more )
}
}
}
}
struct LibraryList: View {
@EnvironmentObject var dataModel: DataModel
var body: some View {
NavigationView { // This navigation here seems to be the issue
List(dataModel.books) { book in
NavigationLink(destination: Text("Book")) { // The crash seems to complain here.
Text("Book name: \(book.name)")
}
}
}
}
}
The working solution
struct ContentView: View {
@EnvironmentObject var dataModel: DataModel
var body: some View {
NavigationView { // Moved navigation view before TabView.
TabView {
LibraryList()
.tabItem {
Label(L10n.library, systemImage: Images.System.library)
}
SettingsList()
.tabItem {
Label(L10n.more, systemImage: Images.System.more )
}
}
}
}
}
struct LibraryList: View {
@EnvironmentObject var dataModel: DataModel
var body: some View {
List(dataModel.books) { book in
NavigationLink(destination: Text("Book")) { // The crash seems to complain here.
Text("Book name: \(book.name)")
}
}
}
}
I hope this helps some of you.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: