SwiftUI:如何在工作表关闭后显示警报?

编程入门 行业动态 更新时间:2024-10-27 00:23:47
本文介绍了SwiftUI:如何在工作表关闭后显示警报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试显示由模式表触发的警报.这是一个小型演示项目:

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:

  • 总是以提醒关闭:
  • Always close with an alert:
  • struct ContentView: View { @State private var showSheet = false @State private var showAlert = false var body: some View { Button("Press") { showSheet = true } .sheet(isPresented: $showSheet, onDismiss: { showAlert = true }) { Button("Close") { showSheet = false } } .alert(isPresented: $showAlert) { Alert(title: Text("Alert")) } } }

  • 关闭并在仅点击按钮时发出警报:
  • Close with an alert on button click only:
  • struct ContentView: View { @State private var showSheet = false @State private var showAlert = false @State private var closeSheetWithAlert = false var body: some View { Button("Press") { showSheet = true closeSheetWithAlert = false } .sheet(isPresented: $showSheet, onDismiss: { showAlert = closeSheetWithAlert }) { Button("Close") { closeSheetWithAlert = true showSheet = false } } .alert(isPresented: $showAlert) { Alert(title: Text("Alert")) } } }

    更多推荐

    SwiftUI:如何在工作表关闭后显示警报?

    本文发布于:2023-11-27 16:52:22,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1638825.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:警报   如何在   工作   SwiftUI

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!