我被这个问题困住了。我有以下从 @Binding 数组构建列表的结构
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 不需要修改 apple。
struct AppleRow: View {
var apple: Apple
var body: some View {
...
}
}
问题出在 ForEach 循环中:如何从元素的绑定(bind)数组创建绑定(bind)元素,当它们将被修改时将修改发送到 AppleList 的父级?
最佳答案
解决方案很简单:
ForEach(apples.indices) { idx in
NavigationLink(destination: AppleDetail(transaction: self.$apples[idx])) {
AppleRow(transaction: apples[idx])
}
}
关于ios - 在 SwiftUI 中通过多个 View 绑定(bind)数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58203127/