Home > Back-end >  SwiftUI TabView how can I load more data on 2nd tab click
SwiftUI TabView how can I load more data on 2nd tab click

Time:12-11

I am working with TabView and would like to add more data when a user clicks for the 2nd time on the same tab item . I have been looking at other examples such as this demo

struct TabViews: View {
    @State var selectedTab: Tab = .MainView
    @State private var firstPass = false

    private var selection: Binding<Tab> { Binding(  // << this !!
        get: { selectedTab },
        set: {
            selectedTab = $0

            print("Tapped!! \(selectedTab)")


            if $0 == .MainView  {

                if firstPass == true {
                    // update list
                    print("Update get more data")

                    firstPass = false  // << reset !!
                    return
                }
                firstPass = true
            }
            if $0 == .MenuView {
                print("2")
                firstPass = false
            }
        }
    )}

    var body: some View {
        TabView(selection: selection) {    // << here !!
            Text("Main View")
                .padding()
                .tabItem {
                    Image(systemName: "1.circle")
                    Text("First")
                }
                .tag(Tab.MainView)

            Text("Menu View")
                .padding()
                .tabItem {
                    Image(systemName: "2.circle")
                    Text("Second")
                }
                .tag(Tab.MenuView)
        }
    }
}
  • Related