In UIKit, it is common to present UIAlertController
for modal pop up alert messages in response to some action.
Is there a modal alert controller type in SwiftUI?
Is there a way to present a UIAlertController
from SwiftUI classes? It seems like this may be possible using UIViewControllerRepresentable
but not sure if that is required?
Use Alert
instead.
import SwiftUI
struct SwiftUIView: View {
@State private var showAlert = false;
var body: some View {
Button(action: { self.showAlert = true }) {
Text("Show alert")
}.alert(
isPresented: $showAlert,
content: { Alert(title: Text("Hello world")) }
)
}
}
Bind to isPresented
in order to control the presentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With