【问题标题】:FullScreenCover not dismissing - SwiftUIFullScreenCover 不关闭 - SwiftUI
【发布时间】:2020-12-16 01:26:12
【问题描述】:

我有一个 .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() 更改游戏 ID 以重新启动时,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)
}

【问题讨论】:

  • 你能把完整的代码贴出来
  • 我现在添加了更多代码!

标签: ios swift swiftui


【解决方案1】:

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

【讨论】:

  • 一旦我在中间放了一些打印语句,它就起作用了!只允许视图在更改 ID 之前关闭!
猜你喜欢
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
  • 2019-10-24
  • 2021-06-04
  • 1970-01-01
相关资源
最近更新 更多