繁体   English   中英

SwiftUI:使用@Binding 关闭模式视图不起作用

[英]SwiftUI: Using @Binding to dismiss a modal view not working

我将@State var 传递给几个视图,在子视图上使用@Binding ,当我最终将变量设置回false 时,有时我的视图不会关闭。

似乎我可以运行articleDisplayed.toggle()但如果我在上方或下方运行额外的 function,它将无法正常工作。

知道这里发生了什么吗?

这是我的代码:

struct HomeView: View {

    @EnvironmentObject var state: AppState

    @State var articleDisplayed = false

    // MARK: - Body

    var body: some View {
        NavigationView {
            ZStack {
                List {
                    ForEach(state.cards, id: \.id) { card in
                        Button(action: {
                            self.articleDisplayed = true // I set it to true here 
                            self.state.activeCard = card
                        }) {
                            HomeCell(
                                card: card,
                                publicationColor: self.state.publication.brandColor
                            )
                        }.sheet(isPresented: self.$articleDisplayed) {
                            SafariQuickTopicView(articleDisplayed: self.$articleDisplayed)
                                .environmentObject(self.state)
                                .environment(\.colorScheme, .light)
                        }
                    }
                }
            }
        }
    }
}

然后在我的SafariQuickTopicView中:

struct SafariQuickTopicView: View {

    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    @EnvironmentObject var state: AppState

    @Binding var articleDisplayed: Bool

    var body: some View {
        NavigationView {
            ZStack(alignment: .bottom) {
              // doesn't matter what's in here
            }
            .navigationBarItems(trailing: passButton)
        }
    }

    private var passButton: some View {
        Button(action: self.state.pass {
                DispatchQueue.main.async {
//                    self.state.removeActiveCardFromState()
                    self.articleDisplayed.toggle() // this will work but adding a second function in here prevents it from working, above or below the toggle.
                }
            }
        }) {
            Text("Pass")
        }
    }

最后,在我的AppState中:

func pass(completion: () -> Void) { // need completion?
        guard let activeCard = activeCard else { return }
        if let index = cards.firstIndex(where: { $0.id == activeCard.id }) {
            activeCard.add(comment: "pass")

            rejectCurrentCard() // Does an async operation with an external API but we don't care about the result

            addRemovedActiveCardToUserDefaults()

            completion()
        }
    }

.sheet移出列表,它必须是每个视图层次结构一个,所以像

List {
    ForEach(state.cards, id: \.id) { card in
        Button(action: {
            self.articleDisplayed = true // I set it to true here 
            self.state.activeCard = card
        }) {
            HomeCell(
                card: card,
                publicationColor: self.state.publication.brandColor
            )
        }
    }
}
.sheet(isPresented: self.$articleDisplayed) {
            SafariQuickTopicView(articleDisplayed: self.$articleDisplayed)
                .environmentObject(self.state)
                .environment(\.colorScheme, .light)
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM