Just to note, I’m using NavigationView as I’m targeting iOS 15.
The following code crashes the preview as soon as I click on a link. However, this code compiles ok and also runs ok on the simulator.
Can anyone help me out on why this is causing the preview to crash?
Is my error with navigation or environment?
My code, starting with @main (I am passing .environmentObject(ItemData()) in PreviewProvider but haven't included preview code below):
@main
struct Spelling_HiveApp: App {
@StateObject private var itemData = ItemData()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(itemData)
}
}
}
My ContentView is:
struct ContentView: View {
@EnvironmentObject var itemData: ItemData
@State private var isShowingPageOne = false
var body: some View {
NavigationView {
HStack {
NavigationLink(destination: PageOne(), isActive: $isShowingPageOne) { EmptyView() }
Button("Page One") {
self.isShowingPageOne = true
}
}
.navigationTitle("Home")
.navigationBarTitleDisplayMode(.inline)
}
.environmentObject(itemData)
}
}
And subsequent structs are as follows:
struct PageOne: View {
@EnvironmentObject var itemData: ItemData
var body: some View {
Form {
NavigationLink(destination: PageTwo()) {
Text("Page Two")
}
}
.navigationTitle("Page One")
.environmentObject(itemData)
}
}
struct PageTwo: View {
@EnvironmentObject var itemData: ItemData
var body: some View {
Form {
NavigationLink(destination: Text("Page Three")) {
Text("Page Three")
}
}
.navigationTitle("Page Two")
.environmentObject(itemData)
}
}
And so my code is complete, my model is:
struct Item: Identifiable, Codable {
var id: Int
var value: String
init(id: Int, value: String) {
self.id = id
self.value = value
}
}
extension Item {
static let sampleData: [Item] =
[
Item(id: 1, value: "A"),
Item(id: 2, value: "B")
]
}
class ItemData: ObservableObject {
@Published var items: [Item] = (Item.sampleData)
}
Any help will be greatly appreciated, thanks!