NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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!

   

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.

   

@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 😖🫤

   

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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

Reply to this topic…

You need to create an account or log in to reply.

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.