首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SwiftUI:如何在关闭工作表后显示警报?

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

Stack Overflow用户
提问于 2020-09-19 11:41:02
回答 1查看 2K关注 0票数 5

我在试着显示一个由模态表触发的警报。下面是一个小演示项目:

代码语言:javascript
复制
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"))
        }
    }
}

单击“按”按钮后,将出现一个带有“关闭警报”按钮的模态表。如果按下这个按钮,工作表就会关闭,什么也不会发生。我希望能显示警报。

似乎隐藏工作表的动画导致了问题,因为SwiftUI在设置showSheet = false后似乎并不认为工作表已关闭。以下是支持这一理论的警告:

上的

演示文稿试图呈现 (来自),后者已经呈现了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-19 11:53:45

您可以使用onDismiss

以下是一些基于何时提出警报的示例:

  1. Always关闭并发出警报:

代码语言:javascript
复制
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"))
        }
    }
}

only按钮上的

  1. 关闭,然后单击按钮

代码语言:javascript
复制
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"))
        }
    }
}
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63968344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档