当变量嵌套在对象中时,如何使用 SwiftUI 将绑定传递给子视图?
How to pass binding to subview with SwiftUI when the variable is nested in an object?
这个有效
import SwiftUI
struct ContentView : View {
@State var val1: Int = 0
var body: some View {
MySubview(val1: $val1)
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView(val1: 0)
}
}
#endif
struct MySubview : View {
@Binding var val1: Int
var body: some View {
return Text("Value = \(val1)")
}
}
但是当变量嵌套在对象中时,这会失败
import SwiftUI
struct MyStruct {
let number: Int
}
struct ContentView : View {
@State var val1 = MyStruct(number: 7)
var body: some View {
MySubview(val1: $val1.number)
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView(val1: 0)
}
}
#endif
struct MySubview : View {
@Binding var val1: Int
var body: some View {
return Text("Value = \(val1)")
}
}
显示错误:Generic parameter 'Subject' could not be inferred
如何将嵌套变量作为绑定传递给子视图?
该错误具有很强的误导性。数字必须是 var,而不是 let:
struct MyStruct {
var number: Int
}
改一下就可以了。
你的代码很好,除了需要 var number: Int
正如 kontiki 指出的那样。
为了帮助理解在视图之间传递绑定,我准备了以下代码,它以稍微不同的方式显示了 @Binding
的使用:
import SwiftUI
struct Zoo { var shed: Shed }
struct Shed { var animals: [Animal] }
struct Animal { var legs: Int }
struct ZooView : View {
@State var zoo = Zoo( shed: Shed(animals:
[ Animal(legs: 2), Animal(legs: 4) ] ) )
var body: some View {
VStack {
Text("Legs in the zoo directly:")
Text("Animal 1 Legs: \(zoo.shed.animals[0].legs)")
Text("Animal 2 Legs: \(zoo.shed.animals[1].legs)")
Divider()
Text("And now with nested views:")
ShedView(shed: $zoo.shed)
}
}
}
struct ShedView : View {
@Binding var shed: Shed
var body: some View {
ForEach(shed.animals.indices) { index in
VStack {
Text("Animal: \(index+1)")
AnimalView(animal: self.$shed.animals[index])
}
}
}
}
struct AnimalView : View {
@Binding var animal: Animal
var body: some View {
VStack {
Text("Legs = \(animal.legs)")
Button(
action: { self.animal.legs += 1 }) {
Text("Another leg")
}
}
}
}
特别是 ShedView
被赋予一个棚屋的绑定,它在棚屋中的动物数组中查找一个动物,并将对该动物的绑定传递给 AnimalView
。
这个有效
import SwiftUI
struct ContentView : View {
@State var val1: Int = 0
var body: some View {
MySubview(val1: $val1)
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView(val1: 0)
}
}
#endif
struct MySubview : View {
@Binding var val1: Int
var body: some View {
return Text("Value = \(val1)")
}
}
但是当变量嵌套在对象中时,这会失败
import SwiftUI
struct MyStruct {
let number: Int
}
struct ContentView : View {
@State var val1 = MyStruct(number: 7)
var body: some View {
MySubview(val1: $val1.number)
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView(val1: 0)
}
}
#endif
struct MySubview : View {
@Binding var val1: Int
var body: some View {
return Text("Value = \(val1)")
}
}
显示错误:Generic parameter 'Subject' could not be inferred
如何将嵌套变量作为绑定传递给子视图?
该错误具有很强的误导性。数字必须是 var,而不是 let:
struct MyStruct {
var number: Int
}
改一下就可以了。
你的代码很好,除了需要 var number: Int
正如 kontiki 指出的那样。
为了帮助理解在视图之间传递绑定,我准备了以下代码,它以稍微不同的方式显示了 @Binding
的使用:
import SwiftUI
struct Zoo { var shed: Shed }
struct Shed { var animals: [Animal] }
struct Animal { var legs: Int }
struct ZooView : View {
@State var zoo = Zoo( shed: Shed(animals:
[ Animal(legs: 2), Animal(legs: 4) ] ) )
var body: some View {
VStack {
Text("Legs in the zoo directly:")
Text("Animal 1 Legs: \(zoo.shed.animals[0].legs)")
Text("Animal 2 Legs: \(zoo.shed.animals[1].legs)")
Divider()
Text("And now with nested views:")
ShedView(shed: $zoo.shed)
}
}
}
struct ShedView : View {
@Binding var shed: Shed
var body: some View {
ForEach(shed.animals.indices) { index in
VStack {
Text("Animal: \(index+1)")
AnimalView(animal: self.$shed.animals[index])
}
}
}
}
struct AnimalView : View {
@Binding var animal: Animal
var body: some View {
VStack {
Text("Legs = \(animal.legs)")
Button(
action: { self.animal.legs += 1 }) {
Text("Another leg")
}
}
}
}
特别是 ShedView
被赋予一个棚屋的绑定,它在棚屋中的动物数组中查找一个动物,并将对该动物的绑定传递给 AnimalView
。