使用Beta4,似乎该错误仍然存在。下面的视图序列(列表,在列表条目上点击时会打开另一个列表)允许只显示ListView
一次; onDisappear
不会被调用,因此showModal
标志会更改,但再次点击时不会触发ListView
的重新显示。因此,对于每个GridCellBodyEntry
,.sheet
演示文稿只能工作一次,然后再也不能工作。
我尝试了一些建议和解决方法,但是都没有用(例如,用NavigationViewModel封装)。我什至试图删除列表,因为有一个假设List
会导致该行为,但是即使这样也没有任何改变。
周围有什么主意吗?
设置:
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)
}
}
}
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")
}
}
}
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 ?? ""))
}
}
}
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 ...
}
在发生任何更改(创建,修改,删除操作)时调用willChange.send()
。
答案 0 :(得分:0)
这是swiftUI PresentaionLink does not work second time
的变体以下简化的代码展示了您遇到的行为(工作表仅显示一次):
import SwiftUI
struct ContentView: View {
@State var isPresented = false
@State var whichPresented = -1
var body: some View {
NavigationView {
List {
ForEach(0 ..< 10) { i in
Button(action: {
self.whichPresented = i
self.isPresented.toggle()
})
{ Text("Button \(i)") }
}.sheet(isPresented: $isPresented, content: {
Text("Destination View \(self.whichPresented)") })
}
}
}
}
当您将.sheet
放在列表或ForEach中时,SwiftUI中似乎存在一个错误。如果将.sheet
移出列表,则应该能够获得正确的行为。
import SwiftUI
struct ContentView: View {
@State var isPresented = false
@State var whichPresented = -1
var body: some View {
NavigationView {
List {
ForEach(0 ..< 10) { i in
Button(action: {
self.whichPresented = i
self.isPresented.toggle()
})
{ Text("Button \(i)") }
}
}
}.sheet(isPresented: $isPresented, content: { Text("Destination View \(self.whichPresented)") })
}
}