gpt4 book ai didi

ios - 如何向全局类添加警报消息

转载 作者:行者123 更新时间:2023-11-28 09:40:36 24 4
gpt4 key购买 nike

我在我的许多 View Controller 中都重复了以下代码。我想将它重构为一个全局类/ View Controller 。但是,我仍然希望它与父级一起显示为当前 View Controller ,以便当我单击“确定”时它将返回到发生错误的当前 View Controller 。该全局类的名称应该是什么,它应该是 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);
}
}

您还可以将 handlercompletion 参数添加到 displayAlertMessage 函数,这将允许您自定义操作处理程序和当警报从调用点完成呈现时会发生什么。

关于ios - 如何向全局类添加警报消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48998836/

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