我已经创建了一个包装,用于创建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()
}
}
}
第二个是当我们在屏幕上边缘滑动时出现的回调。
答案 0 :(得分:1)
您收到UIScreenEdgePanGestureRecognizer
的多条消息-可能的值为:
case possible
case began
case changed
case ended
case cancelled
case failed
因此,您将在.began
上显示对话框,然后在下一条消息上立即将其关闭-我认为是.cancelled
,因为您显示了警报。
在您的handleEdgeSwipe()
函数中,执行以下操作:
if sender.state == .recognized {
print("Screen edge swiped!")
builder.show()
}
您无需在此执行任何其他操作,因为在显示对话框时不会出现边缘滑动。
注意:.recognized
是UIScreenEdgePanGestureRecognizer
的内部变量,如果您不需要它们,可以避免检查不同的状态。
答案 1 :(得分:0)
AlertDialog被释放,整个视图消失,尝试在其他地方初始化AlertDialog,然后使用该实例显示对话框