即使设置了核心数据,也很难理解如何将数据传递给ChildView / DetailViews。
我设置了具有一对多关系的核心数据。
(一个):Bean.entity()<-(很多):Shot.entity()
从BeanList中,我将选择的Bean通过“ bean:bean”传递到BeanDetailView,它可以正常工作。代码:
import SwiftUI
struct BeanList: View {
@FetchRequest(entity: Bean.entity(), sortDescriptors: []) var beans: FetchedResults<Bean>
var body: some View {
NavigationView {
List {
ForEach(beans, id: \.id) { bean in
NavigationLink(destination: BeanDetailView(bean: bean)) {
Image(uiImage: UIImage(data: bean.img ?? self.imageAlt)!)
// deleted the rest, cause not relevant for my problem.
}
}
}
}
}
}
在BeanDetailView中,我使用:“ var bean:Bean”接管选定的bean,它可以工作。它正在列出镜头。
import SwiftUI
struct BeanDetailView: View {
@FetchRequest(entity: Bean.entity(), sortDescriptors: []) var beans: FetchedResults<Bean>
// selected Bean from ListView "BeanDetailView(bean: bean)"
var bean: Bean
var body: some View {
List {
ForEach(bean.shotArray, id: \.self) { shot in
NavigationLink(destination: ShotDetail()) { // <-- Code here <---
Text("\(shot.time)")
// deleted the rest, cause not relevant for my problem.
}
}
}
}
}
现在我的问题:
如何将所选镜头传递给ShotDetail?我尝试了“ ShotDetail(shot:shot)”或“ shot:bean.shotArray”等,但是我没有得到。以及如何接管ShotDetail中选定的Shot?
希望您能帮助我。 非常感谢你, 安德烈