问题描述
当我在模拟器或画布中运行选择器代码时,选择器总是返回第一个选项并带有动画或只是冻结.这发生在上周四/周五.所以我检查了一些旧的简单代码,在那之前它可以工作,但对我来说也不起作用.这是简单的旧代码.它不再适用于 beta 3、4 和 5.
When I run a Picker Code in the Simulator or the Canvas, the Picker goes always back to the first option with an animation or just freezes. This happens since last Thursday/Friday. So I checked some old simple code, where it worked before that and it doesn't work for me there, too.This is the simple old Code. It doesn't work anymore in beta 3, 4 and 5.
struct PickerView : View {
@State var selectedOptionIndex = 0
var body: some View {
VStack {
Text("Option: \(selectedOptionIndex)")
Picker(selection: $selectedOptionIndex, label: Text("")) {
Text("Option 1")
Text("Option 2")
Text("Option 3")
}
}
}
}
在我较新的代码中,我使用了 @ObservedObject
,但在这里它也不起作用.此外,我没有收到任何错误,并且它可以构建和运行.感谢您的指点.
In my newer code, I used @ObservedObject
, but also here it doesn't work.Also I don't get any errors and it builds and runs.Thank you for any pointers.
----EDIT----- 请先看答案
在帮助之后,我可以在所有 Text()
后面使用 .tag()
,例如 Text("Option 1").tag()
,它现在采用初始值并在视图中更新它.如果我像这里一样使用 @ObservedObject
:
After the help, that I could use the .tag()
behind all Text()
like Text("Option 1").tag()
, it now takes the initial value and updates it inside the view. If I use @ObservedObject
like here:
struct PickerView: View {
@ObservedObject var data: Model
let width: CGFloat
let height: CGFloat
var body: some View {
VStack(alignment: .leading) {
Picker(selection: $data.exercise, label: Text("select exercise")) {
ForEach(data.exercises, id: \.self) { exercise in
Text("\(exercise)").tag(self.data.exercises.firstIndex(of: exercise))
}
}
.frame(width: width, height: (height/2), alignment: .center)
}
}
}
}
不幸的是,它没有反映值的变化,如果我在另一个视图中进行这些更改,一个导航链接.而且它似乎不适用于我上面的代码,我在其中使用 firstIndex(of: Exercise)
Unfortunately it doesn't reflect changes on the value, if I make these changes in another view, one navigationlink further. And also it doesn't seem to work with the my code above, where I use firstIndex(of: exercise)
---编辑---现在如果我改变上面的代码Text("\(exercise)").tag(self.data.exercises.firstIndex(of:exercise))
进入Text("\(exercise)").tag(self.data.exercises.firstIndex(of:exercise)!)
因为它不能与可选的一起使用.
---EDIT--- Now the code above works if I changeText("\(exercise)").tag(self.data.exercises.firstIndex(of: exercise))
intoText("\(exercise)").tag(self.data.exercises.firstIndex(of: exercise)!)
because it couldn't work with an optional.
推荐答案
答案总结:
- 使用
.tag()
后面的选项它可以工作.它看起来像下面这样:
- With the
.tag()
behind the Options it works. It would look like following:
Picker(selection: $selectedOptionIndex, label: Text("")) {
ForEach(1...3) { index in
Text("Option \(index)").tag(index)
}
}
- 如果您使用一系列对象,它可能如下所示:
Picker(selection: $data.exercises, label: Text("")) {
ForEach(0..<data.exercises.count) { index in
Text("\(data.exercises[index])").tag(index)
}
}
我不确定它是否有意,此处需要使用 .tag()
,但这至少是一种解决方法.
I am not sure if it is intended, that .tag()
is needed to be used here, but it's at least a workaround.
这篇关于为什么在 swiftui 中绑定到 Picker 不再起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!