【问题标题】:SwiftUI call function on tab view navigation标签视图导航上的 SwiftUI 调用功能
【发布时间】:2021-12-30 09:50:41
【问题描述】:

我想在用户切换TabView 中的选项卡时执行功能。奇怪的是,我似乎无法用.simultaneousGesture 做到这一点。如果用户在选项卡视图中切换选项卡,如何执行功能?

示例代码:

struct ContentView: View {
    @State var selectedTab: Int = 0
    var body: some View {
        NavigationView {
            TabView(selection: $selectedTab) {
                Text("View One")
                .tabItem {
                    Text("View One")
                }
                .simultaneousGesture(TapGesture().onEnded {
                    print("This isn't printing")
                })
            
                Text("View Two")
                    .tabItem {
                        Text("View Two")
                    }
                    .simultaneousGesture(TapGesture().onEnded {
                        print("This isn't printing")
                    })
            
                 Text("View Three")
                    .tabItem {
                        Text("View Three")
                    }
                    .simultaneousGesture(TapGesture().onEnded {
                        print("This isn't printing")
                    })
            }
        }
    }
}

编辑:更新代码

struct ContentView: View {
    @State var selectedTab: Int = 0
    var body: some View {
        NavigationView {
            TabView(selection: $selectedTab) {
                Text("View One")
                    .tabItem {
                        Text("View One")
                }
            
                Text("View Two")
                    .tabItem {
                        Text("View Two")
                    }
            
                Text("View Three")
                    .tabItem {
                        Text("View Three")
                    }
            }
        }
        .onReceive(Just(selectedTab)) { thing in
            print("Tapped!!")
            print("\(thing)")
        }
    }
}

【问题讨论】:

  • 这是否回答了您的问题stackoverflow.com/a/64020773/12299030
  • 我赞成您的回答,因为我认为它可以解决我的问题,但实际上并没有。我的发布者在视图初始化时打印一次,但在更改所选选项卡时不再打印。我会发布更新的代码
  • 像往常一样,@Asperi 答案完美无缺。再看看他的代码。为了使选择起作用,您必须标记选项卡,否则系统不知道哪个是哪个。您在代码中忽略了这一点。在这种情况下,.onReceiveJust 发布者或 .onChange(of:) 都适用。显然,.onChange(of:) 是 iOS 14+。
  • 感谢您指出这一点!我忘了标签

标签: swiftui navigationview tabview


【解决方案1】:

为每个选项卡添加.tag 修饰符,并使用自定义Binding 在setter 中完成工作。

struct ContentView: View {
    @State var selectedTab: Int = 0
    var body: some View {
        NavigationView {
            TabView(selection: Binding(
                get: { selectedTab },
                set: {
                    selectedTab = $0
                    print("switched to tab \(selectedTab)")
                }
            )) {
                Text("View One")
                    .tabItem { Text("1") }
                    .tag(1)
                Text("View Two")
                    .tabItem { Text("2") }
                    .tag(2)
                Text("View Three")
                    .tabItem { Text("3") }
                    .tag(3)
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-20
    相关资源
    最近更新 更多