ios - UIAlertController - 如果第一次解除警报,则不会执行操作

标签 ios swift uialertcontroller uialertaction

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

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

这是出现此错误的特定弹出窗口的代码(没有任何其他弹出窗口受到影响):

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)

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

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

有什么想法吗?

最佳答案

虽然我对这个可重用部分的编码方式有疑问,但可以通过向 skipWalkthroughAction 发送 copy: 消息来解决这个问题。只需做一个:

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:

更好的方法

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

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
    }
}

关于ios - UIAlertController - 如果第一次解除警报,则不会执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35291504/

相关文章:

objective-c - 锁定/解锁 mac osx

xcode - UIAlertController 和堆叠式 UIAlert 按钮

ios - AFNetworking:附加参数作为 PUT 请求的查询字符串

ios - UIDatePicker 在 "done"按钮按下时获取 .date 属性

iphone - UINavigationbar 覆盖 View 顶部

ios - AVAudioPlayer 不在 Swift 中播放音频

macos - 使用 Swift 获取已安装的卷列表?

ios - 阿拉伯文字对齐

ios - 来自 UIAlertController 的键盘延迟关闭

objective-c - 改变位置 UIAlertController (UIAlertControllerStyleActionSheet)