.sheet:仅显示一次,然后不再显示

时间:2019-07-23 19:03:38

标签: swiftui

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

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

周围有什么主意吗?

设置:

  1. 具有此视图的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)
            }
        }
    }
  1. 具有此定义的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")
        }
    }
}
  1. 具有此定义的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 ?? ""))
        }
    }
}
  1. 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()

1 个答案:

答案 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)") })
    }
}