UIAlertController - 如果第一次解除警报,则不会执行操作 [英] UIAlertController - Action not executed if alert dismissed the first time

查看:14
本文介绍了UIAlertController - 如果第一次解除警报,则不会执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在进行的一个项目中,我必须编写一个 UIAlert 帮助程序模块,该模块将在我的 iOS 应用程序中到处显示弹出窗口.弹出窗口被编写为类函数,我可以简单地在代码中的任何地方调用它们(类是静态的,所有函数也是如此).

In a project I'm working on, I had to write a UIAlert helper module that would display popups here and there in my iOS app. The popups are written as class functions that I can simply call anywhere in the code (the class is static and so are all the functions).

我现在遇到了一个非常奇怪的错误,如果您关闭一次警报,然后再次打开它,它的操作将不再起作用(例如,未调用操作处理程序).但是,如果您在第一次显示弹出窗口时单击该操作,它确实有效...

I am now encountering a very weird bug where if you dismiss an alert once, then open it again, its actions don't function anymore (as in, the action handler is not called). It does work if you click the action the first time the popup is displayed, though...

这是发生此错误的特定弹出窗口的代码(不影响其他弹出窗口):

Here is the code for the specific popup for which this bug occurs (no other popups are affected whatsoever):

static func popSkipWalkthrough() {
    let alert = UIAlertController(title: "Skip", message: "whatever", preferredStyle: .Alert)

    alert.addAction(cancelAction)
    alert.addAction(skipWalkthroughAction)
    appDelegate.window!.rootViewController!.presentViewController(alert, animated: true, completion: nil)
}

skipWalkthroughAction 定义如下:

static let skipWalkthroughAction = UIAlertAction(title: "Continue", style: .Default, handler: { (action: UIAlertAction!) -> Void in
    appDelegate.setWindowViewTo("NavCtrl", navigateTo: false)
    CallIn.Settings.didWalkthrough = true
})

cancelAction 定义为:

static let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)

每当您在演练的最后一步按下跳过"按钮时,都会特别显示此弹出窗口...

This popup in particular is shown every time you press the 'skip' button in the last step of a walk-through...

我已经尝试了一些关于这种行为的原因的线索,我认为这可能与弹出窗口没有真正被释放有关,但我现在完全不确定......

I have tried a few leads on what the cause for this behavior is, and I think it might have something to do with the popup not really being deallocated, but I'm not sure at all at this point...

有什么想法吗?

推荐答案

尽管我对这个可重用部分的编码方式有问题,但可以通过发送 copy: 消息到skipWalkthroughAction.只需做一个:

All though I have problems with the way this reusable piece is coded, this problem can be solved by sending a copy: message to skipWalkthroughAction. Simply do a:

static func popSkipWalkthrough() {
    let alert = UIAlertController(title: "Skip", message: "whatever", preferredStyle: .Alert)

    alert.addAction(cancelAction.copy() as! UIAlertAction)
    alert.addAction(skipWalkthroughAction.copy() as! UIAlertAction)
    appDelegate.window!.rootViewController!.presentViewController(alert, animated: true, completion: nil)
}

这应该可以解决它.

您也可以通过将 alert 移至实例级别来解决此问题.你不必发送 copy: 那么.

You can also solve this by moving the alert to instance level. You don't have to send copy: then.

更好的方法

如果您想要真正"可重用的 UIAlertController 体验,最好创建一个 UIViewController 扩展.我的一个项目中有这个:

If you want a "truly" reusable experience of UIAlertController, you are better off creating a UIViewController extension. I have this in one of my projects :

extension UIViewController {
    func showAlertControllerWithTitle(title:String?,message:String?,actions:[UIAlertAction],dismissingActionTitle:String?, dismissBlock:(() -> ())?) -> UIAlertController {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
        if dismissingActionTitle != nil {
            let okAction = UIAlertAction(title: dismissingActionTitle, style: .Default) { (action) -> Void in
                dismissBlock?()
                alertController.dismissViewControllerAnimated(true, completion:nil)
            }
            alertController.addAction(okAction)
        }
        for action in actions {
            alertController.addAction(action)
        }
        self.presentViewController(alertController, animated: true, completion:nil)
        return alertController
    }
}

这篇关于UIAlertController - 如果第一次解除警报,则不会执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆