UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Trying to figure out NavigationView “Preview Crashed”

Forums > SwiftUI

@Mambo  

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!

1      

This crashes for me as well, running in iOS 16 simulator. As you noted, it works in simulated iOS devices.

You probably found a bona-fide bug in the Canvas.

1      

@Mambo  

Hi @Obelix, thanks so much for taking a look at this.

I had posted this to stack overflow too, if interested the link is [https://stackoverflow.com/questions/74003957/swiftui-trying-to-figure-out-navigationview-preview-crashed]. While feedback there helped me resolve my rookie issue, I don't see it resolving the crashes you have gotten with the iOS 16 simulator (I had a stray NavigationView in my preview). My error may have helped you find a bug 😖🫤

1      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.