ios - SwiftUI - 显示警报
问题描述
我希望在用户无需任何操作而快速移动到新页面时显示警报,我使用 NavigationLink
内容视图.swift
struct ContentView: View {
var body: some View {
VStack{
NavigationLink(destination: SecondView()){
Text("Go to second view")
}
}
}
}
SecondView.swift
struct SecondView: View {
@State var showAlert = true
var body: some View {
// i want to show alert when navigate to this view
VStack{
Text("Second View")
.alert(isPresented: $showAlert) {
Alert(title: Text("You are in second view"))
}
}
}
}
你可以帮助我 ?
解决方案
出现时将值更改showAlert
为true VStack
,就像那样
struct SecondView: View {
@State var showAlert = false
var body: some View {
// i want to show alert when navigate to this view
VStack{
Text("Second View")
.alert(isPresented: $showAlert) {
Alert(title: Text("You are in second view"))
}
}.onAppear{
showAlert = true
}
}
}
推荐阅读
- c# - 无法使用 UWP 应用程序访问我系统上的 Word 文档
- php - Laravel 中的文件上传路径不正确
- javascript - 在转到下一个命令之前,无法让 Javascript myFunction() 运行 var requiredFields = document.getElementById
- python - 如何在多个应用程序模板中使用 Django Haystack whoosh 搜索字段
- javascript - 我如何检查该函数是否存在于我的班级中
- xcode - 带有 Safari 视图控制器的客户端证书
- c - C编程中数组中缺少数字
- c# - 如何从源中获取 .pdf 文件以将其复制到目标的每日文件夹下?
- javascript - 用主页上的动态精选帖子内容替换静态内容 - Blogger Blogspot
- css - 我无法处理绝对位置的 z-index