Reputation: 97
I have created a wrapper for creating AlertDialog to mimic how it is done in 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()
}
}
}
As soon as this piece of code is loaded an alert comes to the screen but it disappears fraction of seconds after it comes. I had put several logs but it is confirm that no action handler is executed.
I am calling builder.show()
when edge swipe callback comes.
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()
}
}
}
Second one is the callback that comes when we edge swipe on screen.
Upvotes: 0
Views: 136
Reputation: 77486
You receive more than one message for UIScreenEdgePanGestureRecognizer
- possible values are:
case possible
case began
case changed
case ended
case cancelled
case failed
So, you are showing the dialog on .began
and then immediately dismissing it on the next message -- which, I believe, is .cancelled
because you show the alert.
Inside your handleEdgeSwipe()
func, do this:
if sender.state == .recognized {
print("Screen edge swiped!")
builder.show()
}
You shouldn't have to do anything else there, as you won't get an edge-swipe while the dialog is showing.
Note: .recognized
is an internal var of UIScreenEdgePanGestureRecognizer
that allows you to avoid checking the different states if you don't need them.
Upvotes: 1
Reputation: 2685
AlertDialog is released and whole view is disappearing, try to init AlertDialog somewhere else and then use that instance to display dialog
Upvotes: 0