我陷入了这个问题。我有以下从@Binding Array构建列表的结构
struct AppleList: View {
@Binding var apples: [Apple]
var body: some View {
NavigationView {
List {
ForEach(apples) { apple in
NavigationLink(destination: AppleDetail(apple: $apple)) {
AppleRow(apple: apple)
}
}
}
}
}
}
AppleDetail有一个可在AppleSummary和AppleEditor之间切换的编辑按钮,因此此处将修改苹果。
struct AppleDetail: View {
@Binding var apple: Apple
var body: some View {
...
}
}
AppleRow不需要修改苹果。
struct AppleRow: View {
var apple: Apple
var body: some View {
...
}
}
问题出在ForEach循环中:如何从元素的绑定数组中创建一个绑定元素,使它们在被修改时会将修改内容发送给AppleList的父级?
答案 0 :(得分:0)
解决方案非常简单:
ForEach(apples.indices) { idx in
NavigationLink(destination: AppleDetail(transaction: self.$apples[idx])) {
AppleRow(transaction: apples[idx])
}
}