Reputation: 63
I want an alert to be displayed when the user is moved to a new page without any action and quickly I use NavigationLink
ContentView.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"))
}
}
}
}
you can help me ?
Upvotes: 0
Views: 2430
Reputation: 501
// 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
}
}
Now here we are going to show alert using the APAlert class
import SwiftUI
struct ContentView: View {
var body: some View {
VStack{
Text("SHOW ALERT").onTapGesture {
APAlert.shared.showAlert(title: "Title", message: "Error", buttonTitle: "OK")
}
}
}
}
Reference :- https://github.com/Arvindcs/APAlertView
Upvotes: 0
Reputation: 1794
Change showAlert
value to true when VStack
appeared , like that
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
}
}
}
Upvotes: 2