在 SwiftUI 视图中一起分配 属性 个值?
Assign property values together in SwiftUI view?
我正在尝试将视图中的属性绑定在一起,但找不到更好的专门针对它的方法。这就是我正在做的:
struct ContentView: View {
@ObservedObject var model: MyModel
@State var selectedID: Int
var body: some View {
Picker("Choose", selection: $selectedID) {
Text("Abc").tag(0)
Text("Def").tag(1)
Text("Ghi").tag(2)
}
.onChange(of: model.item?.selectedID) {
selectedID = [=11=]
}
}
}
是否有更好的方法将属性绑定在一起?
如果它是关于 单向流 那么我在提供的快照中看到唯一需要的更改是制作相似的类型。其他都没问题。
struct ContentView: View {
@ObservedObject var model: MyModel
@State var selectedID: Int? // << make optional as well !!
...
我正在尝试将视图中的属性绑定在一起,但找不到更好的专门针对它的方法。这就是我正在做的:
struct ContentView: View {
@ObservedObject var model: MyModel
@State var selectedID: Int
var body: some View {
Picker("Choose", selection: $selectedID) {
Text("Abc").tag(0)
Text("Def").tag(1)
Text("Ghi").tag(2)
}
.onChange(of: model.item?.selectedID) {
selectedID = [=11=]
}
}
}
是否有更好的方法将属性绑定在一起?
如果它是关于 单向流 那么我在提供的快照中看到唯一需要的更改是制作相似的类型。其他都没问题。
struct ContentView: View {
@ObservedObject var model: MyModel
@State var selectedID: Int? // << make optional as well !!
...