Ios SwiftUI-显示警报
我希望在用户无需任何操作就移动到新页面并快速使用NavigationLink时显示警报 ContentView.swiftIos SwiftUI-显示警报,ios,swift,xcode,swiftui,Ios,Swift,Xcode,Swiftui,我希望在用户无需任何操作就移动到新页面并快速使用NavigationLink时显示警报 ContentView.swift struct ContentView: View { var body: some View { VStack{ NavigationLink(destination: SecondView()){ Text("Go to second view")
struct ContentView: View {
var body: some View {
VStack{
NavigationLink(destination: SecondView()){
Text("Go to second view")
}
}
}
}
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"))
}
}
}
}
斯威夫特酒店
struct ContentView: View {
var body: some View {
VStack{
NavigationLink(destination: SecondView()){
Text("Go to second view")
}
}
}
}
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"))
}
}
}
}
您可以帮助我吗?当出现
VStack
时,将showarert
值更改为true,如下所示
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
}
}
}
现在,我们将使用APAlert类显示警报
import SwiftUI
struct ContentView: View {
var body: some View {
VStack{
Text("SHOW ALERT").onTapGesture {
APAlert.shared.showAlert(title: "Title", message: "Error", buttonTitle: "OK")
}
}
}
}
参考:-