Я создал оболочку для создания AlertDialog, чтобы имитировать, как это делается в Android
import Foundation
protocol AlertDialogDelegate {
func onAlertPositiveActionClicked(alertDialog: AlertDialog)
func onAlertNegativeActionClicked(alertDialog: AlertDialog)
}
final class AlertDialog {
private var controller: UIViewController?
private var alert: UIAlertController?
private var title: String = ""
private var message: String = ""
private var posAct: UIAlertAction?
private var negAct: UIAlertAction?
private var neuAct: UIAlertAction?
private var cancelable: Bool?
private var delegate: AlertDialogDelegate?
private init() {
}
private func setController(controller: UIViewController) {
self.controller = controller
}
private func setListener(listener: AlertDialogDelegate) {
self.delegate = listener
}
private func callPositiveActionListener() {
assert(self.delegate != nil)
self.delegate!.onAlertPositiveActionClicked(alertDialog: self)
}
private func callNegativeActionListener() {
assert(self.delegate != nil)
self.delegate!.onAlertNegativeActionClicked(alertDialog: self)
}
private func setTitle(title: String) {
self.title = title
}
private func setMessage(message: String) {
self.message = message
}
private func setPosAct(action: UIAlertAction) {
self.posAct = action
}
private func setNegAct(action: UIAlertAction) {
self.negAct = action
}
private func setNeuAct(action: UIAlertAction) {
self.neuAct = action
}
private func setCancelable(isCancelable: Bool) {
self.cancelable = isCancelable
}
private func build() {
alert = UIAlertController(title: self.title, message: self.message, preferredStyle: .alert)
if (self.neuAct != nil) {
alert!.addAction(self.neuAct!)
}
if (self.negAct != nil) {
alert!.addAction(self.negAct!)
}
if (self.posAct != nil) {
alert!.addAction(self.posAct!)
}
self.controller!.present(alert!, animated: true, completion: nil)
}
func dissmiss() {
if (self.cancelable!) {
self.alert!.dismiss(animated: true, completion: nil)
}
}
final class Builder {
private let alertDialog: AlertDialog
init(controller: UIViewController) {
self.alertDialog = AlertDialog()
print("call coming")
self.alertDialog.setController(controller: controller)
self.alertDialog.setListener(listener: controller as! AlertDialogDelegate)
}
func setTitle(title: String) -> Builder {
print("call coming1")
self.alertDialog.setTitle(title: title)
return self
}
func setMessage(message: String) -> Builder {
print("call coming2")
self.alertDialog.setMessage(message: message)
return self
}
func setPositiveAction(title: String) -> Builder {
print("call coming3")
let action = UIAlertAction(title: title, style: .destructive, handler: { _ in
print("call coming")
self.alertDialog.callPositiveActionListener() })
self.alertDialog.setPosAct(action: action)
return self
}
func setNegativeAction(title: String) -> Builder {
let action = UIAlertAction(title: title, style: .cancel, handler: { _ in self.alertDialog.callNegativeActionListener() })
self.alertDialog.setNegAct(action: action)
return self
}
func setNeutralAction(title: String) -> Builder {
let action = UIAlertAction(title: title, style: .default, handler: { _ in self.alertDialog.dissmiss() })
self.alertDialog.setNeuAct(action: action)
return self
}
func setCancelable(isCancelable: Bool) -> Builder {
self.alertDialog.setCancelable(isCancelable: isCancelable)
return self
}
func show() {
self.alertDialog.build()
}
}
}
Как только этот фрагмент кода загружен, на экран приходит предупреждение, но оно исчезает через доли секунды после его появления. Я поставил несколько журналов, но это подтверждает, что никакой обработчик действий не выполняется.
Я звоню builder.show()
, когда приходит обратный вызов.
override func viewDidLoad() {
super.viewDidLoad()
builder = AlertDialog.Builder(controller: self)
.setTitle(title: "Caution")
.setMessage(message: "To cancel this payment request, please go to.")
.setCancelable(isCancelable: false)
.setPositiveAction(title: "Ok")
.setNegativeAction(title: "Cancel")
}
extension OPController : EdgeSwipeGesture {
internal func handleEdgeSwipe(sender: UIScreenEdgePanGestureRecognizer) {
// TODO: Handle back press kind of action in here
if (!backAlertShown) {
self.builder!.show()
backAlertShown = true
} else {
assert(self.delegate != nil)
self.finish()
}
}
}
Второй - это обратный вызов, который происходит, когда мы проводим по экрану.