我希望当用户在没有任何操作的情况下移动到新页面并快速使用 NavigationLink 时显示警报
ContentView.swift
:- initialization(main).
list_of_questions(['what is ipv4', 'what is router']).
in_list_of_questions(X) :-
list_of_questions(L),
member(X, L).
Res :- in_my_list_of_elements('what is ipv4 ?').
%write(list_of_questions).
main :- write(Res).
SecondView.swift
struct ContentView: View {
var body: some View {
VStack{
NavigationLink(destination: SecondView()){
Text("Go to second view")
}
}
}
}
你能帮我吗?
答案 0 :(得分:1)
在出现 showAlert
时将 VStack
值更改为 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
}
}
}
答案 1 :(得分:0)
// MARK:- CUSTOM ALERT CLASS
import SwiftUI
private struct AlertView: View {
let loaderBackgroundColor: Color = .secondary
let loaderCornerRadius: CGFloat = 10.0
/// parameter to hide and show loader
@Binding var isShowing: Bool
let titleText: String
let messageText: String
let buttonText: String
var body: some View {
ZStack(alignment: .center) {
VStack(spacing: 10) {
if titleText.count > 0 {
Text(titleText)
.foregroundColor(.black)
.fontWeight(.bold)
.padding(.bottom, 10)
}
Text(messageText)
.font(.system(size: 14))
.foregroundColor(.gray)
Spacer()
Button(action: {
APAlert.shared.remove()
}) {
Text(buttonText)
.font(.system(size: 14))
.foregroundColor(.white)
.fontWeight(.bold)
}
.frame(width: 100, height: 40)
.background(Color.black)
.cornerRadius(20.0)
}
.padding(EdgeInsets(top: 40, leading: 20, bottom: 30, trailing: 20))
.frame(width: 300, height: 200)
.background(Color.white)
.cornerRadius(10.0)
.shadow(color: Color(.sRGBLinear, white: 0, opacity: 0.13), radius: 10.0)
}
}
}
public class APAlert {
public static var shared = APAlert()
private init() { }
private var popupWindow: AlertWindow?
public func showAlert(title: String = "Error", message: String = "Request could not be processed due to a server error. The request may succeed if you try again.", buttonTitle: String = "Ok") {
setAlertBody(title: title, message: message, buttonTitle: buttonTitle)
}
/// function to remove loader from screen.
public func remove() {
removeAlert()
}
}
// MARK: - AlertWindow
private class AlertWindow: UIWindow {
}
private extension APAlert {
func setAlertBody(title: String = "", message: String = "", buttonTitle: String = "") {
let windowScene = UIApplication.shared
.connectedScenes
.filter { $0.activationState == .foregroundActive }
.first
if let windowScene = windowScene as? UIWindowScene {
popupWindow = AlertWindow(windowScene: windowScene)
popupWindow?.frame = UIScreen.main.bounds
popupWindow?.backgroundColor = .clear
popupWindow?.rootViewController = UIHostingController(rootView: AlertView(isShowing: .constant(true), titleText: title, messageText: message, buttonText: buttonTitle))
popupWindow?.rootViewController?.view.backgroundColor = UIColor.gray.withAlphaComponent(0.6) //.opacity(0.5)
popupWindow?.makeKeyAndVisible()
}
}
/// Remove loader from screen
func removeAlert() {
let alertwindows = UIApplication.shared.windows.filter { $0 is AlertWindow }
alertwindows.forEach { (window) in
window.removeFromSuperview()
}
popupWindow = nil
}
}
现在我们将使用 APAlert 类显示警报
import SwiftUI
struct ContentView: View {
var body: some View {
VStack{
Text("SHOW ALERT").onTapGesture {
APAlert.shared.showAlert(title: "Title", message: "Error", buttonTitle: "OK")
}
}
}
}