0

I have NavigationStack with NavigationPath:

   ...
   @State var path = NavigationPath()
   ...
   var body: some View {
    NavigationStack(path: $path) {
        VStack() {
            MyView() {
                path.append("Edit")
            }
        }
        .navigationDestination(for: String.self) { view in
            if view == "Edit" {
                EditView {
                    path.removeLast()
                }
            }
        }
    }


    struct EditView: View {
    var backAction: (() -> (Void))?
    var body: some View {
        VStack() {
           Text("Edit")
        }
        .navigationBarBackButtonHidden(true)
        .toolbar {
            ToolbarItem(placement: .navigationBarLeading) {
            Button {
                backAction?()
            } label: {
                Image("backArrow")
            }
        }
    }

The app is crashing on path.removeLast() with:

EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

How should I implement the navigation with NavigationPath?

A-Tech
  • 806
  • 6
  • 22
ArisRS
  • 1,362
  • 2
  • 19
  • 41
  • For some reason it looks like the path is empty when you try to remove the last component. You have other options: 1) Pass the `path` as a `Binding` to the subview; this will give full control to that view (and you might want to check for empty arrays before removing components) - 2) Use `presentationMode` from the `Environment`, so the subview can simply dismiss itself, without accessing the `NavigationPath`. – HunterLion May 19 '23 at 07:45
  • tried second approach, my View passed to navigation stack also, and while I perform dismiss, it is just pop to root view. This is why I tried to modify navigation path. – ArisRS May 19 '23 at 07:48
  • For 1 approach the same crash – ArisRS May 19 '23 at 07:51
  • Note, works well for me in my tests, on MacOS 13.4, Xcode 14.3, tested on real ios 16 devices (not Previews), and macCatalyst. It could be different on older systems. – workingdog support Ukraine May 19 '23 at 08:00
  • you could try this `if !path.isEmpty { path.removeLast() }` – workingdog support Ukraine May 19 '23 at 08:36

0 Answers0