使用Beta4,似乎该错误仍然存在。以下视图序列(列表,其中点击列表条目可打开另一个列表)允许呈现ListView
正好一次;这onDisappear
从未被调用过,所以showModal
标志发生变化,但不会触发重新显示ListView
再次点击时。所以,对于每个GridCellBodyEntry
, the .sheet
演示文稿只工作一次,然后就不再工作。
我尝试了几种建议和解决方法,但没有一个有效(例如,用 NavigationViewModel 封装)。我什至尝试删除该列表,因为有一个假设List
导致这种行为,但即使这样也没有改变任何东西。
周围有什么想法吗?
设置:
- A
GridCellBody
有了这个观点:
var body: some View {
GeometryReader { geometry in
VStack {
List {
Section(footer: self.footerView) {
ForEach(self.rawEntries) { rawEntry in
GridCellBodyEntry(entityType: rawEntry)
}
}
}
.background(Color.white)
}
}
}
- A
GridCellBodyEntry
有了这个定义:
struct GridCellBodyEntry: View {
let entityType: EntityType
let viewModel: BaseViewModel
init(entityType: EntityType) {
self.entityType = entityType
self.viewModel = BaseViewModel(entityType: self.entityType)
}
@State var showModal = false {
didSet {
print("showModal: \(showModal)")
}
}
var body: some View {
Group {
Button(action: {
self.showModal.toggle()
},
label: {
Text(entityType.localizedPlural ?? "")
.foregroundColor(Color.black)
})
.sheet(isPresented: $showModal, content: {
ListView(showModal: self.$showModal,
viewModel: self.viewModel)
})
}.onAppear{
print("Profile appeared")
}.onDisappear{
print("Profile disappeared")
}
}
}
- A
ListView
有了这个定义:
struct ListView: View {
// MARK: - Private properties
// MARK: - Public interface
@Binding var showModal: Bool
@ObjectBinding var viewModel: BaseViewModel
// MARK: - Main view
var body: some View {
NavigationView {
VStack {
List {
Section(footer: Text("\(viewModel.list.count) entries")) {
ForEach(viewModel.list, id: \.objectID) { item in
NavigationLink(destination: ItemView(),
label: {
Text("\(item.objectID)")
})
}
}
}
}
.navigationBarItems(leading:
Button(action: {
self.showModal = false
}, label: {
Text("Close")
}))
.navigationBarTitle(Text(viewModel.entityType.localizedPlural ?? ""))
}
}
}
- The
BaseViewModel
(摘抄):
class BaseViewModel: BindableObject {
/// The binding support.
var willChange = PassthroughSubject<Void, Never>()
/// The context.
var context: NSManagedObjectContext
/// The current list of typed items.
var list: [NSManagedObject] = []
// ... other stuff ...
}
where willChange.send()
每当发生更改(创建、修改、删除操作)时调用。