1

I am having trouble with a return from a navigation view within a tabbed view. My project has a Settings tab where the user may select via navigation link "View Entries". And from there another navigation link to "Add New Entry". Returning from Add New Entry should bring you to View Entries but instead is return another level to the Setting Menu.

I am seeing a warning on the console stating "trying to pop to a missing destination at /Library/Caches/com.apple...". Using the tabbed view sample code at SwiftUI NavigationView trying to pop to missing destination (Monoceros?) I no longer get the "pop-to-missing-destination" warning but I still have the same problem with the navigation return.

The sample code below is ready to run and test in Xcode 12.

In the sample code below, tap settings and select the navigation view "View Entries". This would be a screen where entries are displayed in a list. Tapping the plus button is where new entries could be added. The textfield on the "Add New Entry" screen doesn't do anything. Clicking the Save or Back buttons should return you to "View Entries" screen but instead returns you to the Setting Menu. The Save button uses presentationMode.wrappedValue.dismiss to dismiss the view.

The fact that two different version of the tab view logic didn't have any impact on my navigation view return logic leads me to believe that I just have some kind on plain old bug in my navigation view logic but I sure don't see one. The sample code below is using the standard tab view logic.

struct ContentView: View {
    
    @State private var selection = 0
    
    var body: some View {
        
        NavigationView {
            TabView (selection: $selection) {
                
                HomeView()
                    .tabItem {
                        Label("Home", systemImage: "house")
                    }.tag(1)
                
                AView()
                    .tabItem {
                        Label("A", systemImage: "a.circle")
                    }.tag(2)
                
                
                BView()
                    .tabItem {
                        Label("B", systemImage: "b.circle")
                    }.tag(3)
                
                
                SettingsView()
                    .tabItem {
                        Label("Settings", systemImage: "gearshape")
                    }.tag(4)
            }
        }
    }
}


struct HomeView: View {
    
    var body: some View {
        
        Text("Home Screen")
    }
}


struct AView: View {
    
    var body: some View {
        
        Text("A Screen")
    }
}


struct BView: View {
    
    var body: some View {
        
        Text("B Screen")
    }
}


struct SettingsView: View {
    
    var body: some View {
        
        VStack (alignment: .leading) {
            List {
            Text("Settings")
                .font(.title)
                .fontWeight(.bold)
                .padding(.leading, 15)
            
                NavigationLink(destination: SetAView()) {Text("View Entries")}
            }
        }
        .font(.body)
    }
}


struct SetAView: View {
    
    var body: some View {
        
        List {
            Text("View Entries")
                .padding(.vertical, 10)
            Text("Normally entires would be displayed here")
            Text("Should return here upon adding new entry")
                .padding(.vertical, 10)
            Text("Click the + button to add new entry")
            
        }
        .navigationBarItems(trailing: NavigationLink (destination: AddTestView()) {
            Image(systemName: "plus")
                .resizable()
                .foregroundColor(Color(.systemBlue))
                .frame(width: 18, height: 18)
        } // body
        )
    }
}


struct AddTestView: View {
    
    @Environment(\.presentationMode) var presentationMode
    @State private var catSelect: String = ""
    
    var body: some View {
        NavigationView {
            VStack {
                Form {
                    
                    Section {
                        TextField("Enter Entry Name", text: $catSelect)
                            .padding(.horizontal, 20)
                            .keyboardType(.default)
                    }
                }
            }
            
            .navigationBarTitle(Text("Add new Entry"), displayMode: .inline)
            .navigationViewStyle(StackNavigationViewStyle())
            .navigationBarItems(trailing: Button(action: {
                self.presentationMode.wrappedValue.dismiss()
            }) {
                Text ("Save")
                
            })
        }
    }
}
Galen Smith
  • 299
  • 2
  • 14
  • I changed the overall Tab View to a Navigation View to see if the Tab View is causing the problems with the navigation links. When I go to Settings -> View Entries -> + (Add New Entry) and then click the Back button the system correctly returns to the View Entries. But when in Add New Entry and click Save, the system skips the View Entries screen and returns to the Settings Menu. – Galen Smith Apr 11 '21 at 03:03
  • Hi, I've placed `NavigationView` on top of `TabView`, but then when navigating deep into DetailsView, tab bar disappears. Do you know how to fix it, so the tab bar always displayed? – MikeMaus Jul 21 '21 at 06:00
  • When navigation view is used with tab view the tab bar normally disappears when navigating to a lower level. The tab bar reappears when the back button is pressed. – Galen Smith Jul 23 '21 at 23:14

1 Answers1

0

After considerable analysis I discovered in my actual code that I had two copies of NavigationView––nothing wrong with the TabView code. Removing the one NavigationView not in contentView then caused several functions from working so rebuilt them from scratch. Now have everything working with TabView and NaigationView including the back buttons.

Galen Smith
  • 299
  • 2
  • 14