Ios UIAlertController在出现后的一段时间内自动关闭
我已经创建了一个用于创建AlertDialog的包装器,以模拟它在Android中的实现方式Ios UIAlertController在出现后的一段时间内自动关闭,ios,swift,uialertcontroller,uialertaction,Ios,Swift,Uialertcontroller,Uialertaction,我已经创建了一个用于创建AlertDialog的包装器,以模拟它在Android中的实现方式 import Foundation protocol AlertDialogDelegate { func onAlertPositiveActionClicked(alertDialog: AlertDialog) func onAlertNegativeActionClicked(alertDialog: AlertDialog) } final class AlertDialo
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,然后使用该实例来显示对话框。您收到的
UIScreenedGepangestureRecognitor
的多条消息-可能的值为:
case possible
case began
case changed
case ended
case cancelled
case failed
因此,您将在上显示对话框。开始
,然后在下一条消息中立即取消该对话框。我认为,这是。取消
,因为您显示了警报
在handleEdgeSwipe()函数中,执行以下操作:
if sender.state == .recognized {
print("Screen edge swiped!")
builder.show()
}
您不必在那里执行任何其他操作,因为在显示对话框时不会得到边缘滑动
注意:.recognized
是UIScreenedGePangestureRecognitizer
的一个内部变量,允许您在不需要的情况下避免检查不同的状态。尝试在视图控制器类中使生成器
对象成为全局对象。如果有效的话,我会解释的。我试着在viewDidLoad里面做了。。但行为是不公平的same@Thullo-显示用于触发警报的代码。按按钮点击快速测试按预期显示对话框,并等待确定或取消选择。@DonMag检查我已更新的帖子。使用按钮时,警报是持续的,但我希望在发生回移边缘滑动时发出警报。我尝试在viewDidLoad中创建生成器对象,但行为相同,将其创建为变量并保留是的,我在viewDidLoad中将其创建为变量,然后在边缘滑动时访问它happens@Thullo在构建器中应该有一个build()方法,该方法返回AlertDialog的实例,并将其作为变量保留在视图控制器中。您可以在AlertDialog上的deinit()中打印一些内容,以检查它是否正在被释放。这项功能可以正常工作,但我仍然不明白为什么在我没有显式执行时会丢失它。什么是self.finish()
do?dissmiss of view controllerprivate func finish(){self.navigationController(动画:true,完成:nil)}