HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – UIAlertController – 如果警报第一次被解除,则不执行动作大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在我正在进行项目中,我必须编写一个UIAlert帮助程序模块,它将在我的iOS应用程序中显示弹出窗口.弹出窗口是作为类函数编写的,我可以简单地在@L_675_4@中的任何地方调用(类是静态的,所有函数都是静态的).

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

以下是发生此错误的特定弹出窗口的@L_675_4@(没有其他弹出窗口受到影响):

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)
}@H_675_12@ 
 

skipWalkthroughAction定义如下:

static let skipWalkthroughAction = UIAlertAction(title: "ConTinue",style: .Default,handler: { (action: UIAlertAction!) -> Void in
    appDelegate.setWindowViewTo("NavCtrl",navigateTo: falsE)
    CallIn.SetTings.didWalkthrough = true
})@H_675_12@ 
 

而CAncelAction定义为:

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

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

我已经尝试了一些关于这种行为的原因,并且我认为它可能与弹出窗口没有真正被解除分配有关,但我现在还不确定…

有任何想法吗 ?

解决方法

然我对这个可重用片段的编码方式有问题,但是这个问题可以通过发送一个copy:message来解决skipWalkthroughAction.只需做一个

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

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

这应该解决它.

您还可以通过将警报移动到实例级别来解决此问题.你不必发送副本:然后.

更好的方法

如果您想要一个“真正”可重用的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,completion:nil)
        return alertController
    }
}@H_675_12@

大佬总结

以上是大佬教程为你收集整理的ios – UIAlertController – 如果警报第一次被解除,则不执行动作全部内容,希望文章能够帮你解决ios – UIAlertController – 如果警报第一次被解除,则不执行动作所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。