gpt4 book ai didi

ios - 警报 View 的常用功能?

转载 作者:行者123 更新时间:2023-11-28 11:01:12 25 4
gpt4 key购买 nike

我想在我的 Controller 类中显示警报 View 。所以我创建了一个通用功能来显示警报并响应它的操作按钮。

Commonfunctions.swift

我已经创建了一个函数,如下所示

 func showActionAlertView(title:String,message:String,vc:UIViewController) -> Void {

let Alert = UIAlertController(title: "Warning", message: NSLocalizedString("Alert_Delete", comment: ""), preferredStyle: UIAlertControllerStyle.alert)

Alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action: UIAlertAction!) in
Constant.commonfunction.showLoader(withMessage: "Loading")

}))

Alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
print("Handle Cancel Logic here")
}))

vc.present(Alert, animated: true, completion: nil)
}

并在 Commonfunctions.swift 中创建了一个协议(protocol)。

protocol alertDelegate
{
func okAction(controller:UIViewController)
func cancelAction(controller:UIViewController)
}

在 Controller 类中我添加了这个

MyController:UIViewController,UITableViewDelegate,UITableViewDataSource,CLLocationManagerDelegate,alertDelegate
{
var delegate:alertDelegate! = nil
}

这里是回调函数

 func okAction(controller: UIViewController) {

print("Ok Action")

}
func cancelAction(controller: UIViewController) {

print("Cncel Action")

}

我在下面显示这样的警报

  Constant.commonfunction.showActionAlertView(title: NSLocalizedString("Success", comment: ""), message: NSLocalizedString("CreateProperty_Alert_created", comment: ""), vc: self)

我无法调用 okAction 和 cancelAction 方法。告诉如何实现回调。

最佳答案

你需要像这样创建一个委托(delegate)的对象

var delegate: alertDelegate?

通过委托(delegate),你可以像这样调用这个函数

在调用此方法时,您还需要将委托(delegate)指定为 self showActionAlertView

像这样 delegate = vc

Alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action: UIAlertAction!) in
Constant.commonfunction.showLoader(withMessage: "Loading")
delegate?.okAction(controller: vc)
}))

Alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
print("Handle Cancel Logic here")
// Call delegate for Cancel Action Here
delegate?.cancelAction(controller: vc)
}))

已编辑

这样做

class Utility : NSObject {

var delegate: alertDelegate?

func showActionAlertView(title:String,message:String,vc:UIViewController) -> Void {
let Alert = UIAlertController(title: "Warning", message: NSLocalizedString("Alert_Delete", comment: ""), preferredStyle: UIAlertControllerStyle.alert)

Alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action: UIAlertAction!) in
self.delegate?.okAction(controller: vc)

}))

Alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
print("Handle Cancel Logic here")
self.delegate?.cancelAction(controller: vc)
}))

vc.present(Alert, animated: true, completion: nil)
}
}

然后像这样使用它

func showAlert() {
let vcUtility = Utility()
vcUtility.delegate = self
vcUtility.showActionAlertView(title: "Message", message: "Message", vc: self)

}

func okAction(controller: UIViewController) {
print("Ok")
}
func cancelAction(controller: UIViewController) {
print("Cancel")
}

关于ios - 警报 View 的常用功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40482291/

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