滑动时 SwiftUI onChange() 事件在 TabView 上不起作用
SwiftUI onChange() event doesn't work on TabView when swiping
我想在 TabView 的活动选项卡更改时更改文本的值。我尝试使用 onChange(of: activeTab, perform: {})
来更改存储文本的状态变量的值,但似乎从未调用给 onChange()
的闭包。
TabView(selection: $activeTab) {
ForEach(exampleData) {dayMenu in
DayMenuView(dayMenu: dayMenu)
.padding([.leading, .bottom, .trailing])
}
}
.onChange(of: activeTab, perform: { index in
print(activeTab)
withAnimation {
activeDate = dates[1]
}
})
文本视图
Text(activeDate)
状态变量
let dates = ["Montag", "Dienstag"]
@State private var activeTab: Int = 0
@State private var activeDate = "Montag"
提供的代码不可测试,但我认为这是因为 activeTab
未更改,因为未识别标签视图,由于标签,所以尝试
TabView(selection: $activeTab) {
ForEach(exampleData.indices, id: \.self) { index in
DayMenuView(dayMenu: exampleData[index])
.padding([.leading, .bottom, .trailing])
.tag(index)
}
}
.onChange(of: activeTab, perform: { index in
我想在 TabView 的活动选项卡更改时更改文本的值。我尝试使用 onChange(of: activeTab, perform: {})
来更改存储文本的状态变量的值,但似乎从未调用给 onChange()
的闭包。
TabView(selection: $activeTab) {
ForEach(exampleData) {dayMenu in
DayMenuView(dayMenu: dayMenu)
.padding([.leading, .bottom, .trailing])
}
}
.onChange(of: activeTab, perform: { index in
print(activeTab)
withAnimation {
activeDate = dates[1]
}
})
文本视图
Text(activeDate)
状态变量
let dates = ["Montag", "Dienstag"]
@State private var activeTab: Int = 0
@State private var activeDate = "Montag"
提供的代码不可测试,但我认为这是因为 activeTab
未更改,因为未识别标签视图,由于标签,所以尝试
TabView(selection: $activeTab) {
ForEach(exampleData.indices, id: \.self) { index in
DayMenuView(dayMenu: exampleData[index])
.padding([.leading, .bottom, .trailing])
.tag(index)
}
}
.onChange(of: activeTab, perform: { index in