gpt4 book ai didi

ios - 如何为 UIAlertController(ActionSheet) 创建全局函数以在许多项目类中访问

转载 作者:行者123 更新时间:2023-11-28 21:15:00 24 4
gpt4 key购买 nike

我想在 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 中可以有多个 Action ,它们也有不同的标题和不同的样式。

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

谢谢!

最佳答案

扩展 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)

关于ios - 如何为 UIAlertController(ActionSheet) 创建全局函数以在许多项目类中访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41606298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com