Чтобы отобразить действие оповещения в Swift, вы можете использовать различные методы в зависимости от целевой версии iOS. Я предложу вам несколько подходов:
Метод 1: UIAlertController (iOS 8 и более поздние версии)
let alertController = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default) { _ in
// Handle OK button action
}
alertController.addAction(okAction)
UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil)
Метод 2: UIAlertView (не поддерживается в iOS 9)
let alertView = UIAlertView(title: "Alert Title", message: "Alert Message", delegate: self, cancelButtonTitle: "OK")
alertView.show()
Метод 3: оповещение SwiftUI (iOS 13 и более поздние версии)
import SwiftUI
struct ContentView: View {
@State private var showingAlert = false
var body: some View {
Button(action: {
showingAlert = true
}) {
Text("Show Alert")
}
.alert(isPresented: $showingAlert) {
Alert(title: Text("Alert Title"), message: Text("Alert Message"), dismissButton: .default(Text("OK")))
}
}
}
Метод 4. Сторонние библиотеки
Существуют также сторонние библиотеки, которые предоставляют дополнительные параметры настройки и функции для отображения оповещений в Swift, например SCLAlertView, SwiftMessages или PMAlertController.