show an alert swiftUI code example
Example 1: show alert swiftui
struct ContentView: View {
@State var showsAlert = false
var body: some View {
Button(action: {
self.showsAlert.toggle()
}) {
Text("Show Alert")
}
.alert(isPresented: self.$showsAlert) {
Alert(title: Text("Hello"))
}
}
}
Example 2: create alert in swift
class ViewController: UIViewController {
@IBAction func showAlertButtonTapped(_ sender: UIButton) {
let alert = UIAlertController(title: "My Title", message: "This is my message.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}