UIAlertController 出现后自动关闭一小部分时间
UIAlertController automatically closes fraction of time after it appears
我创建了一个用于创建 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()
}
}
}
第二个是边缘滑动屏幕的回调
AlertDialog 已释放,整个视图正在消失,请尝试在其他地方初始化 AlertDialog,然后使用该实例显示对话框
您收到了不止一封 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()
}
您不必在那里做任何其他事情,因为在显示对话框时您不会得到 edge-swipe。
注意:.recognized
是 UIScreenEdgePanGestureRecognizer
的内部变量,它允许您在不需要时避免检查不同的状态。
我创建了一个用于创建 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()
}
}
}
第二个是边缘滑动屏幕的回调
AlertDialog 已释放,整个视图正在消失,请尝试在其他地方初始化 AlertDialog,然后使用该实例显示对话框
您收到了不止一封 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()
}
您不必在那里做任何其他事情,因为在显示对话框时您不会得到 edge-swipe。
注意:.recognized
是 UIScreenEdgePanGestureRecognizer
的内部变量,它允许您在不需要时避免检查不同的状态。