繁体   English   中英

FullScreenCover 不关闭 - SwiftUI

[英]FullScreenCover not dismissing - SwiftUI

我有一个 .fullScreenCover 当我到达我的应用程序中的某个点时打开。 当我想关闭它并重置应用程序时,我关闭视图并将 ContentView ID 设置为新的 UUID()。 这是我的主要应用程序代码:

class AppState: ObservableObject {
    static let shared = AppState()

    @Published var gameID = UUID()
}

@main
struct RugbyAppApp: App {
    
    @StateObject var appState = AppState.shared
    
    var body: some Scene {
        WindowGroup {
            ContentView().id(appState.gameID)
        }
    }
}

这应该在我每次打开 ContentView 时生成一个新 ID。 当我使用按钮中的另一个 UUID() 更改 gameID 以重新启动时, presentationMode.wrappedValue.dismiss()停止工作,这意味着我无法再回到该视图。

Button("New Game"){
    AppState.shared.gameID = UUID() //Line x
    presentationMode.wrappedValue.dismiss()
}

根据 Cod3rMax 的评论,这就是我调用 fullScreenCover 的方式:

Button(action: {endGameAlert.toggle()}) {
    TopPanelButton(buttonText: "End Game", buttonColour: Color.red, buttonWidth: geometry.size.width/10, buttonHeight: geometry.size.width/30)
}
.alert(isPresented: $endGameAlert){
    Alert(title: Text("Are You Sure?"), message: Text("Do you want to end the game?"),
        primaryButton: Alert.Button.default(Text("End Game"), action: {
            stopWatchManager.endGame()                               
            processor.textReport(homeTeam: homeTeam, awayTeam: awayTeam)                                 
            exportToPDF(homeTeam: homeTeam, awayTeam: awayTeam, events: processor.matchEvents)
            showEnd.toggle()
                                                    
        }),
        secondaryButton: Alert.Button.cancel(Text("Cancel"), action: {})
    )
}
}
.fullScreenCover(isPresented: $showEnd){
    EndGameScreen(homeTeam: homeTeam, awayTeam: awayTeam)
}

看起来您在关闭封面之前正在更改 state。 这意味着 swiftui 将重建视图层次结构并从根本上使您的演示模式无效。 颠倒您的操作顺序应该可以解决您的问题。

暂无
暂无

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

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