每次更新 ContentView 都会重新创建 FullScreenCover 中的 ChildView

时间:2021-01-17 17:21:24

标签: swift swiftui fullscreen swiftui-list

我有一个关于下一个行为的问题。

我有 ContentView 带有传递相应视图模型的视图列表。用户可以点击某个视图。此时将根据传递的类型显示全屏模态对话框。没关系。 有时我的视图模型正在更新,整个 ContentView 将被重新加载。问题是:调用 fullScreenCover 并重新创建 ChildEventView。如何防止重新创建 ChildEventView

struct ContentView: View {

    @State private var fullScreenType: FullScreenType?

    // some stuff

    var body: some View {
        ScrollView {
            LazyVStack {
                ForEach(eventListViewModel.cardStates.indices, id: \.self) { index in
                    let eventVM = eventListViewModel.eventVMs[index]
                    EventCardView(eventViewModel: eventVM, eventId: $selectedEvent.eventId) {
                        self.fullScreenType = .type1
                    }
                    // some other views
                }
            }
        }  
        .fullScreenCover(item: $fullScreenType, onDismiss: {
            self.fullScreenType = nil
        }, content: { fullScreenType in
            switch fullScreenType {
                case .type1:
                return ChildEventView(selectedEvent.eventId).eraseToAnyView()
                // some other cases
            } 
        })
    }
}

0 个答案:

没有答案