【问题标题】:SwiftUI TabView how can I load more data on 2nd tab clickSwiftUI TabView 如何在第二个选项卡单击时加载更多数据
【发布时间】:2021-12-09 05:06:03
【问题描述】:

我正在使用 TabView 并希望在用户第二次单击同一选项卡项时添加更多数据。我一直在查看其他示例,例如 SwiftUI TabView On Click Action,但是我无法让它工作。我在下面有一个简单的例子(完整代码)。基本上,如果标签已经在 MainView 上并且用户再次单击它,我想点击 print("Update get more data") 代码。我使用了 OnReceive 方法,但是如果在同一个选项卡中单击超过 1 次,则该方法不起作用。我也尝试在 TabView 之外使用 onTapGesture 但似乎没有任何建议会很好,因为我正在学习 SwiftUI 并且我使用的是 3.0 版

  *TabView(selection: $selectedTab) {
      ... 
    }.onTapGesture {
        print(selectedTab) // This always give MainView 
    }*

我也试过上面的,它总是给 MainView

import SwiftUI
import Combine

struct TabViews: View {
    @State var selectedTab: Tab = .MainView
    @State private var firstPass = false
    
    var body: some View {
        
        TabView(selection: $selectedTab) {
            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)
            
        }
        
        .onReceive(Just(selectedTab)) {
            print("Tapped!! \(selectedTab)")
            
 
            if $0 == .MainView  {
                
                if firstPass == true {
                    // update list
                    print("Update get more data")
                }
                firstPass = true
            }
            if $0 == .MenuView {
                print("2")
            }
        }
       
        
    }
}

extension TabViews {
    enum Tab: Hashable {
        case MainView
        case MenuView
        case RankingView
    }
}

struct TabView_Previews: PreviewProvider {
    static var previews: some View {
        TabViews()
    }
}

【问题讨论】:

    标签: swift swiftui swiftui-tabview


    【解决方案1】:

    对于您的方案,解决方案是使用代理绑定(拦截每次点击),如引用问题的第二个链接所示。

    使用 Xcode 13 / iOS 15 测试

    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)
            }
        }
    }
    

    【讨论】:

    • 非常感谢您的工作
    猜你喜欢
    • 2021-07-05
    • 1970-01-01
    • 2020-12-03
    • 2012-10-18
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    相关资源
    最近更新 更多