【问题标题】:How to make a global function for UIAlertController(ActionSheet) to access in many classes of project如何为 UIAlertController(ActionSheet) 创建一个全局函数以在多个项目类中访问
【发布时间】:2017-01-12 06:17:30
【问题描述】:

我想在NSObject 类中为UIAlertCotroller 创建一个函数。这样我就可以在任何类中访问此功能。我已尝试使用以下代码:

open class func showActionSheet(_ delegate: UIViewController, message: String, strtittle: String, handler: ((UIAlertController) -> Void)! = nil)
    {
        let actionSheetController: UIAlertController = UIAlertController(title: strtittle, message: message, preferredStyle: UIAlertControllerStyle.actionSheet)

        if handler == nil{
            actionSheetController.addAction(UIAlertAction(title: "Default(Off)" , style: .default , handler:{ (UIAlertAction)in
            }))
        }
        else{
            actionSheetController.addAction(UIAlertAction(title: "Default(Off)" , style: .default , handler:{ (UIAlertAction)in

            }))
        }

        delegate.present(actionSheetController, animated: true, completion: {
            print("completion block")
        })
    }

这是我制作的函数,但问题是 ActionSheet 中可以有多个动作,它们也有不同的标题和不同的样式。

问题:如何实现这个功能?请帮忙。

谢谢!

【问题讨论】:

  • 谢谢您,我认为这仅适用于 AlertController(警报类型)。我正在寻找 AlertController(Action sheet)。
  • 你只需要改变它,这就是为什么我写你可以这样尝试。您也可以为动作标题添加一个字符串数组参数。
  • 这是否意味着我必须知道最大操作数?
  • 类似func buildAlertInfoWithFavButton(withTitle title: String?, andMessage message: String?, withActionTitles titles:[String], withHandler handler: [((UIAlertAction) -> Void)]?) -> UIAlertController { 传递动作标题数组。

标签: ios swift3 uialertcontroller uiactionsheet


【解决方案1】:

制作 UIAlertController 的扩展

extension UIAlertController{

func AlertWithTextField(_ view:UIViewController) -> UIAlertController{

    let actionSheetController: UIAlertController = UIAlertController(title: "Action Sheet", message: message, preferredStyle: UIAlertControllerStyle.actionSheet)


        actionSheetController.addAction(UIAlertAction(title: "No" , style: .default , handler:{ (UIAlertAction)in

        }))
       actionSheetController.addAction(UIAlertAction(title: "Yes" , style: .default , handler:{ (UIAlertAction)in

       }))


    view.present(actionSheetController, animated: true, completion: {
        print("completion block")
    })
 return actionSheetController
}
}

从你的 ViewController 调用它

  let alert = UIAlertController()
  alert.AlertWithTextField(self)

【讨论】:

  • 我正在寻找 UIAlertControllerStyle.actionsheet。
  • 你可以用 Actionsheet 代替 alert
  • 我可以在任何类中调用这个方法吗?
  • 是的,创建新的基础类 New File > Swift File > name Of File 并粘贴扩展名的答案。
  • 好的,谢谢。还有一个问题,如何给出不同数量的动作、动作的标题和风格?根据要求,我在不同的班级中有不同类型的行动表。所以,每个动作表都有动作的数量、动作的标题和它们的风格。所以,我想为此创建全局函数。
【解决方案2】:

我已经通过使用 Swift - How to present ViewController when tapping button in a custom AlertController 找到了我的问题的解决方案

他们正在遵循我必须做的修改来实现我的目标。代码如下:

在控制器类中:

 Alert.showActionSheet(self, message: "Save incoming media for this chat",strtittle: "",actionTittle: ["Default(Off)","Always","Never","Cancel"],
                                        actionStyle:  [.default,.default,.default,.cancel] ,
                                        withHandler:  [defaultHandler, alwaysHandler, neverHandler, cancelHandler])

func defaultHandler(action: UIAlertAction) {
        //Add code of present
        print("DefaultHandler")
    }

    func alwaysHandler(action: UIAlertAction) {
        //Add code of present
        print("alwaysHandler")

    }

    func neverHandler(action: UIAlertAction) {
        //Add code of present
         print("neverHandler")
}

func cancelHandler(action: UIAlertAction) {
    //Add code of present
     print("cancelHandler")
}

在 NSObject 类中:

open class func showActionSheet(_ delegate: UIViewController, message: String, strtittle: String, actionTittle: [String], actionStyle: [UIAlertActionStyle], withHandler handler: [((UIAlertAction) -> Void)]?)
    {
        var actionSheetController: UIAlertController = UIAlertController()

        if message != "" || strtittle != ""
        {
            actionSheetController = UIAlertController(title: strtittle, message: message, preferredStyle: UIAlertControllerStyle.actionSheet)
        }

        for i in 0..<actionTittle.count
        {
            actionSheetController.addAction(UIAlertAction(title: actionTittle[i],
                                                          style: actionStyle[i],
                                                          handler: handler?[i]))
        }

        delegate.present(actionSheetController, animated: true, completion: nil)

    }

使用它,我可以为操作表提供一些操作、它们的标题和样式。而且我也可以在每个类中简单地调用这个方法。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 2015-04-16
    相关资源
    最近更新 更多