Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating Selection from Another View #21

Open
aralez-g0lem opened this issue Jul 18, 2023 · 3 comments
Open

Updating Selection from Another View #21

aralez-g0lem opened this issue Jul 18, 2023 · 3 comments

Comments

@aralez-g0lem
Copy link

How I can updated selected State if I'm navigating to view (presented TabBar), but not from TabBar, like from button inside of another view, or using NavigationLink in another view.

Thank you

View with TabBar

struct GeneralView View {
    
    enum Item: Int, Tabbable {
        case home = 0
        case letters
        case heritage
        case bookmarks
        case third
        
        
        var icon: String {
            switch self {
                case .letters: return "textformat"
                case .heritage: return "command"
                case .home: return "house.fill"
                case .third: return "quote.bubble"
                case .bookmarks: return "bookmark.fill"
               
            }
        }
        
        var title: String {
            switch self {
                case .letters: return "Letters"
                case .heritage: return "Heritage"
                case .home: return "Arageel"
                case .third: return "Third"
                case .bookmarks: return "Bookmarks"
               
            }
        }
    }
    
    @State private var selection: Item = .home
    @State private var visibility: TabBarVisibility = .visible
    
    
    var body: some View {
        
        TabBar(selection: $selection, visibility: $visibility) {
            LettersGridView()
                .tabItem(for: Item.letters)
            
            HeritageZineHomeView()
                .environmentObject(RSSReader())
                .tabItem(for: Item.heritage)
            HomeView()
                .tabItem(for: Item.home)
            PBHomeView()
                .tabItem(for: Item.third)
            
            PBHomeView()
                .tabItem(for: Item.bookmarks)
        }
        .tabBar(style: CustomTabBarStyle())
        .tabItem(style: CustomTabItemStyle())
        .ignoresSafeArea(.all)
    }
}

Another View

    var stickyHeaderView: some View {
        HStack {
            Text("Heritage").modifier(MainStoryTitle(color: "BlackDark"))
            Spacer()
            NavigationLink (
                destination:
                    HeritageHomeView()
            ) {
                Image(systemName: "arrow.up.forward")
                    .foregroundColor(Color("Orange"))
            }
        }
        .padding(.horizontal, 30)

    }
@dkk
Copy link

dkk commented Jul 18, 2023

Use a @Binding var selection: Item in Another View and pass it $selection. This will allow you to update the state.

@aralez-g0lem
Copy link
Author

Reci

Use a @Binding var selection: Item in Another View and pass it $selection. This will allow you to update the state.

Receiving error

Type 'Binding<Item>' has no member 'home'

@dkk
Copy link

dkk commented Jul 18, 2023

that is just telling that you probably removed home from your enum or that it is taking some other Item type instead of your enum (the naming is very generic).

It would be something like this:

  struct GeneralView View {
    @State private var selection: Item = .home
  
    var body: some View {        
      ZStack {
          TabBar(selection: $selection, visibility: $visibility) {
          // ... some more code....
          }

         StickyHeaderView(selection: $selection)
      }
    }
    // ... some more code....
struct StickyHeaderView: View {
  @Binding var selection: Item

  var body: some View {
    Button("Selection to letters") {
                selection = .letters
            }
  }

dkk added a commit to dkk/TabBar that referenced this issue Jul 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants