Assume a singleton class that has an enum about a login state as follows:
@Published var loginState: LoginState = .unknown
The singleton class instance is assigned as an @ObservedObject
in the View to get changes from other @Published
variables as follows:
struct LoginView: View {
@ObservedObject var loginDelegate = Singleton.shared
How do I trigger an Alert in a View using something like the following, when the singleton loginState
is set to a certain enum case (for example .error
)?
}.alert(isPresented: <something> ) { ... }
Try the following
}.alert(isPresented: .constant(loginDelegate.loginState == .error)) { ... }
but you will need to reset loginDelegate.loginState
back to, say, .unknown
in alert close button action, otherwise alert might re-appear immediately.