我正在尝试显示由模式表触发的警报.这是一个小型演示项目:
I'm trying to show an alert which is triggered by a modal sheet. Here's a small demo project:
import SwiftUI struct ContentView: View { @State private var showSheet = false @State private var showAlert = false var body: some View { Button("Press") { showSheet = true } .sheet(isPresented: $showSheet) { Button("Close with alert") { showSheet = false showAlert = true } } .alert(isPresented: $showAlert) { Alert(title: Text("Alert")) } } }点击按"后按钮,会出现一个带有Close with alert"按钮的模式表.如果按下此按钮,工作表将关闭并且没有任何反应.我希望显示警报.
After clicking on the "Press" button, a modal sheet appears with a button "Close with alert". If this button is pressed the sheet closes and nothing happens. I expect to have the alert shown.
似乎是隐藏工作表的动画导致了这个问题,因为在设置 showSheet = false 后,SwiftUI 似乎并不认为工作表已关闭.出现以下警告,支持该理论:
It seems that the animation of hiding the sheet is causing the issue as SwiftUI doesn't seem to consider the sheet as closed after setting showSheet = false. The following warning appears which is supporting this theory:
[Presentation] 尝试展示 <SwiftUI.PlatformAlertController:0x7fbbab012200>在<TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier_:0x7fbbaa60b7d0>(从<TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier_:0x7fbbaa60b7d0>) 已经呈现<TtGC7SwiftUI22SheetHostingControllerVS_7AnyView:0x7fbbaa413200>.
[Presentation] Attempt to present <SwiftUI.PlatformAlertController: 0x7fbbab012200> on <TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier_: 0x7fbbaa60b7d0> (from <TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier_: 0x7fbbaa60b7d0>) which is already presenting <TtGC7SwiftUI22SheetHostingControllerVS_7AnyView: 0x7fbbaa413200>.
推荐答案您可以使用onDismiss.
以下是一些基于何时您想显示警报的示例:
Here are some examples based on when do you want to present an alert:
更多推荐
SwiftUI:如何在工作表关闭后显示警报?
发布评论