Ios 如何为UIAlertController(ActionSheet)创建一个全局函数以在多个项目类中访问
我想在Ios 如何为UIAlertController(ActionSheet)创建一个全局函数以在多个项目类中访问,ios,swift3,uialertcontroller,uiactionsheet,Ios,Swift3,Uialertcontroller,Uiactionsheet,我想在NSObject类中为UIAlertCotroller创建一个函数。这样我就可以在任何类中访问这个函数。我已尝试使用以下代码: open class func showActionSheet(_ delegate: UIViewController, message: String, strtittle: String, handler: ((UIAlertController) -> Void)! = nil) { let actionSheetContro
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中可能有很多动作,它们也有不同的标题和风格
问题:如何实现此功能?请帮忙
谢谢 对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
}
}
let alert = UIAlertController()
alert.AlertWithTextField(self)
从ViewController调用此选项
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
}
}
let alert = UIAlertController()
alert.AlertWithTextField(self)
对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
}
}
let alert = UIAlertController()
alert.AlertWithTextField(self)
从ViewController调用此选项
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
}
}
let alert = UIAlertController()
alert.AlertWithTextField(self)
我用这个找到了解决问题的方法 他们正在进行修改,我必须这样做,以实现我的目标。代码如下: 在控制器类中:
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")
}
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)
}
在NSObject类中:
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")
}
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)
}
open-class-func-showActionSheet(u-delegate:UIViewController,message:String,strtitle:String,actionTitle:[String],actionStyle:[UIAlertActionStyle],withHandler-handler:[((UIAlertAction)->Void)]?)
{
var actionSheetController:UIAlertController=UIAlertController()
如果消息!=“strtittle!”
{
actionSheetController=UIAlertController(标题:strTitle,消息:message,首选样式:UIAlertControllerStyle.actionSheet)
}
对于0..中的i,我已通过使用此函数找到问题的解决方案
以下是我为实现目标所做的修改。以下是代码:
在控制器类中:
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")
}
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)
}
在NSObject类中:
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")
}
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)
}
open-class-func-showActionSheet(u-delegate:UIViewController,message:String,strtitle:String,actionTitle:[String],actionStyle:[UIAlertActionStyle],withHandler-handler:[((UIAlertAction)->Void)]?)
{
var actionSheetController:UIAlertController=UIAlertController()
如果消息!=“strtittle!”
{
actionSheetController=UIAlertController(标题:strTitle,消息:message,首选样式:UIAlertControllerStyle.actionSheet)
}
对于0中的i..您可以这样尝试谢谢,我认为这仅适用于AlertController(警报类型)。我正在寻找AlertController(操作表)。您只需要更改它,这就是我写的原因。您可以这样尝试。您还可以为操作标题添加一个字符串数组参数。这是否意味着我必须知道操作的最大数量?类似于以下方式func buildAlertInfoWithFavButton(带标题:string?,消息消息:string?,带操作标题:[String],withHandler-handler:[((UIAlertAction)->Void)]?)->UIAlertController{
还可以传递动作标题数组。您可以这样尝试。谢谢,我想这只是针对AlertController(警报类型)的。我正在寻找AlertController(动作表)。您只需要更改它,这就是我写的原因。您可以这样尝试。您还可以为操作标题添加一个字符串数组参数。这是否意味着我必须知道操作的最大数量?类似于以下方式func buildAlertInfoWithFavButton(带标题:string?,消息消息:string?,带操作标题:[String],withHandler处理程序:[((UIAlertAction)->Void)]?)->UIAlertController{ /COD>动作标题的PASS数组。我正在寻找AcExaltMutuxSimult.ActScEnter。你可以用Accon SeTeCon替换警报吗?我在任何类中调用这个方法。是的,创建新类的基础新文件> SWIFT文件>文件名并粘贴扩展答案。谢谢,还有一个问题:如何给出不同数量的动作,动作?n的标题及其样式?根据要求,我在不同的类中有不同类型的操作表。因此,每个操作表都有操作数、操作标题及其样式。因此,我想为此创建全局函数。我正在寻找UIAlertControllerStyle.actionsheet。您可以用actionsheet替换alert。我可以将此方法称为I吗任何一个类?是的,创建新的基础新文件> SWIFT文件>文件名和扩展的粘贴答案。好的,谢谢。还有一个问题:如何给出不同数量的动作、动作的标记和它们的样式?根据要求,我在不同的类中有不同类型的动作表。因此,每个动作表都有多个动作。,动作的标题和他们的风格。所以,我想做一个全局函数。