0
votes

I'm using Navigation View inside TabView and the problem is that if I an on Tab A and with NavigationView I open other Views, when changing from tab A to B and after a while I came back to tab A I dosen't reload tab A from beginning but it show the last View open with NavitagionLink. The problem is that in each View I'm getting data from a DB and because of that if shows an empty view.

My ContentView looks like this:

import SwiftUI

struct ContentView: View {
        
    @ObservedObject var appState = AppState()
    
    @State var currentTab : Tab
    
    
    var body: some View {
        
        TabView(selection: $appState.currentTab) {
            
            NavigationView {
                HomeView(appState: appState)
                              
            }
                .tabItem {
                    if appState.currentTab == .home {
                        Image(systemName: "house.fill")
                    } else {
                        Image(systemName: "house")
                    }

                    Text(LocalizedStringKey("HomeTabMenu"))
                    
                }.tag(Tab.home)
        
            NavigationView {
                SearchView()
            }
                .tabItem {
                    if appState.currentTab == .search {
                        Image(systemName: "magnifyingglass.circle.fill")
                    } else {
                        Image(systemName: "magnifyingglass")
                    }
                    Text(LocalizedStringKey("SearchTabMenu"))
                }.tag(Tab.search)
          
            NavigationView {
                AddItemView(appState: appState)
            }
                .tabItem {
                    if appState.currentTab == .add {
                        Image(systemName: "plus.circle.fill")
                    } else {
                        Image(systemName: "plus.circle")
                    }
                    Text(LocalizedStringKey("SellTabMenu"))
                }.tag(Tab.add)
            
            NavigationView {
                ShoppingCartFavoritesView()
            }
                .tabItem {
                    if appState.currentTab == .favorites {
                        Image(systemName: "cart.fill")
                    } else {
                        Image(systemName: "cart")
                    }
                    Text(LocalizedStringKey("CartTabMenu"))
                }.tag(Tab.favorites)
            
            NavigationView {
                ProfileView(appState: appState)
            }
                .tabItem {
                    if appState.currentTab == .profile {
                        Image(systemName: "person.fill")
                    } else {
                        Image(systemName: "person")
                    }
                    Text(LocalizedStringKey("ProfileTabMenu"))
                }.tag(Tab.profile)
            
        }//End TabView
        .accentColor(Color("ColorMainDark"))
        
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(currentTab: Tab.home)
    }
}

class AppState: ObservableObject {
    @Published var currentTab : Tab = .home
}

enum Tab {
    case home, search, add, favorites, profile
}

And if I open SearchView()

struct SearchView: View {
   var body: some View {
       NavigationLink(destination: View_2(id: "ABC")){
                            Text("ABC")
                        }
  }
}
struct View_2: View {

   @ObservedObject var performSearchProducts = PerformSearchInProducts() 
   var id  : String

   var body: some View {
      ScollView{

       ForEach(performSearchProducts.products) { product in
                     Text(product.name)
                        }
     }.onAppear(perform: {
            self.performSearchProducts.searchSubCategory(id: id)
        })
  }
}

If in SearchView I'm on View_2() and the I open another Tab, when I come back to tab SearchView it doesn't show the SearchView(), but it remains on View_2() with the back button in navigation bar.

How can I make to show SearchView() and not keep the state of NavigationLink ?

1

1 Answers

0
votes

It's the default behavior. Attach id to TabView.

}//End TabView
      .accentColor(Color("ColorMainDark"))
        .id(appState.currentTab) //<--Here