gpt4 book ai didi

swiftui - .sheet : Shows only once and then never again

转载 作者:行者123 更新时间:2023-12-03 19:26:03 24 4
gpt4 key购买 nike

使用 Beta4 时,似乎该错误仍然存​​在。以下 View 序列(一个列表,点击列表条目打开另一个列表)允许显示 ListView恰好一次; onDisappear永远不会被调用,所以 showModal flag 改变,但不会触发 ListView 的重新显示再次点击时。因此,对于每个 GridCellBodyEntry , .sheet演示文稿只工作一次,然后再也不会。

我尝试了一些建议和解决方法,但都没有奏效(例如,用 NavigationViewModel 封装)。我什至试图删除列表,因为有一个假设是 List导致这种行为,但即使这样也没有改变任何东西。

有什么想法吗?

设置:

  • 一个 GridCellBody此 View :
  • 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()每当发生变化(创建、修改、删除操作)时调用。

    最佳答案

    这是 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 时,SwiftUI 中似乎存在一个错误。在 List 或 ForEach 中。如果您移动 .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)") })
    }
    }

    关于swiftui - .sheet : Shows only once and then never again,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57170669/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com