如何将警报消息添加到全局类 [英] How to add alert message to a global class

查看:76
本文介绍了如何将警报消息添加到全局类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的许多view controllers中都重复了以下代码.我想将其重构为全局类/视图控制器.但是,我仍然希望将其与父视图一起显示为当前视图控制器,以便当我单击确定"时,它将返回到发生错误的当前视图控制器.该全局类的名称应该是什么,并且它应该是UIViewController的子类?

    //--------------------------------------
    // MARK: - Alert Error Message
    //--------------------------------------
    func displayAlertMessage(title:String,message:String)
    {
        let alertMessage = UIAlertController(title: title, message: message, preferredStyle:UIAlertControllerStyle.alert);

        let okAction = UIAlertAction(title:"OK", style: .default, handler:nil);

        alertMessage.addAction(okAction);

        self.present(alertMessage, animated: true, completion: nil);
    }

解决方案

您可以创建UIViewController扩展名并在此实现此方法.这将允许您从任何UIViewController子类中调用它.

extension UIViewController {

    func displayAlertMessage(title: String, message: String)
    {
        let alertMessage = UIAlertController(title: title, message: message, preferredStyle:UIAlertControllerStyle.alert);

        let okAction = UIAlertAction(title:"OK", style: .default, handler:nil);

        alertMessage.addAction(okAction);

        self.present(alertMessage, animated: true, completion: nil);
    }
}

您还可以在displayAlertMessage函数中添加一个handlercompletion参数,这将允许您自调用点自定义动作处理程序以及警报显示完毕后会发生什么. /p>

I have the following code repeated in many of my view controllers. I would like to refactor it into a global class/view controller. However, I still want it to be shown with the parent as the current view controller so that when I click OK it will return to the current view controller where the error occurred. What should the name of that global class be and should it subclass UIViewController?

    //--------------------------------------
    // MARK: - Alert Error Message
    //--------------------------------------
    func displayAlertMessage(title:String,message:String)
    {
        let alertMessage = UIAlertController(title: title, message: message, preferredStyle:UIAlertControllerStyle.alert);

        let okAction = UIAlertAction(title:"OK", style: .default, handler:nil);

        alertMessage.addAction(okAction);

        self.present(alertMessage, animated: true, completion: nil);
    }

解决方案

You can create a UIViewController extension and implement this method there. This will allow you to call it from any UIViewController subclass.

extension UIViewController {

    func displayAlertMessage(title: String, message: String)
    {
        let alertMessage = UIAlertController(title: title, message: message, preferredStyle:UIAlertControllerStyle.alert);

        let okAction = UIAlertAction(title:"OK", style: .default, handler:nil);

        alertMessage.addAction(okAction);

        self.present(alertMessage, animated: true, completion: nil);
    }
}

You could also add a handler and completion parameters to the displayAlertMessage function, which would allow you to customize the both the action handler and what happens when the alert finishes being presented, from the calling point.

这篇关于如何将警报消息添加到全局类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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