我尝试了很多不同的方法,但没有一个有效,当我尝试添加 .onChange 时,我收到此错误
Instance method 'onChange(of:perform:)' requires that 'Binding<Int>' conform to 'Equatable'
Run Code Online (Sandbox Code Playgroud)
我这样定义 currentTab
@State var currentTab = 0;
Run Code Online (Sandbox Code Playgroud)
这段代码给出了错误
.onChange(of: $currentTab, perform: { newValue in
print(newValue)
})
Run Code Online (Sandbox Code Playgroud)
为了使 onChange 工作,您需要做的不仅仅是拍打 onChange,您还需要包含适当的标签,如本示例
struct ContentView: View {
@State var value: Int = 1
var body: some View {
TabView(selection: $value) {
Text("View One").tabItem { Text("One") }.tag(1)
Text("View Two").tabItem { Text("Two") }.tag(2)
Text("View Three").tabItem {Text("Three") }.tag(3)
}
.onChange(of: value) { val in
print("---> val: \(val)")
}
}
}
Run Code Online (Sandbox Code Playgroud)
注意:标签类型应与“值”类型匹配。
| 归档时间: |
|
| 查看次数: |
1251 次 |
| 最近记录: |