Zack Shapiro
Zack Shapiro

Reputation: 6998

Tried to pop to a view controller that doesn't exist in SwiftUI

I'm getting a strange crash from pretty normal navigation on my SwiftUI app

I have a simple tab view :

struct FFTabView: View {
    var body: some View {
        TabView {
            LibraryView2()
        }
        .navigationBarBackButtonHidden(true)
        .navigationBarHidden(true)
        .navigationBarTitle("", displayMode: .inline)
    }
}

// MARK: -

struct LibraryView2: View {

    var body: some View {
        VStack {
            NavigationLink(destination: Foo()) {
                Text("go to foo")
            }
        }
        .tabItem {
            Image(systemName: "square.grid.2x2.fill")
            Text("Skill Library")
        }
    }

}

struct Foo: View {
    var body: some View {
        Text("foo view")
    }
}

When I go back via my nav bar, from Foo I get a crash: Tried to pop to a view controller that doesn't exist

Any idea what's going on here? I can't find anything related to this and SwiftUI so figured I'd post. Thanks

Upvotes: 7

Views: 1853

Answers (1)

John M.
John M.

Reputation: 9463

Although you didn't specify, I assume your FFTabView is wrapped in a NavigationView somewhere.

Ultimately, then, your view hierarchy looks like

NavigationView {
    TabView {
        NavigationLink {
            ...
        }
    }
}

If you restructure your view hierarchy so it's like

TabView {
    NavigationView {
        NavigationLink {
            ...
        }
    }
}

The crash doesn't happen.

Edit: I have confirmed that it is related to the regression/bug discussed in this answer, introduced in Xcode 11.2. Your original code works fine in Xcode 11.1.

Upvotes: 5

Related Questions