Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

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

like image 719
Zack Shapiro Avatar asked Nov 06 '19 19:11

Zack Shapiro


People also ask

How do I pop to a specific view in SwiftUI?

ContentView -> View1 -> View2 And from View2 you want to pop to the Root view.

What is a navigation controller?

NavController manages app navigation within a NavHost . Apps will generally obtain a controller directly from a host, or by using one of the utility methods on the Navigation class rather than create a controller directly. Navigation flows and destinations are determined by the navigation graph owned by the controller.


1 Answers

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.

like image 172
John M. Avatar answered Jun 09 '23 02:06

John M.