ios - 在全局 swift 文件中添加警报控制器?
问题描述
我正在尝试在文件以外的 swift 文件中创建一个警报框UIViewController
。但我无法创建它。
extension NetworkManager {
func showAlert(message: String,from:UIViewController, title: String = "") {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(OKAction)
from.present(alertController, animated: true, completion: nil)
}
}
上面的代码是用于实现 alertcontroller 但我不知道如何传递我需要呈现的视图控制器,所以需要帮助。
解决方案
extension UIViewController {
func showAlert(message: String, title: String = "") {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion: nil)
}
}
并像这样使用
from.showAlert(message:"Your message", title: "Title")
推荐阅读
- excel - 将数据从工作表中的单元格复制到另一个工作表中的下一个可用行
- c# - 根据用户角色重定向到某些网页不起作用
- c++ - 为什么 c++ 使用 throw() 关键字来指示函数不会抛出任何异常
- c# - 登录 sql vs files vs aws,这在 c# 应用程序中更快
- java - 龙目岛建造者模式问题
- postgresql - Postgres 远程连接
- javascript - 如何使用 cypress 同时进行 2 个用户之间的端到端测试交互
- objective-c - 如何在 Xcode 11 中启动没有故事板 >= iOS 13 的新项目?
- aurelia - Aurelia 组合,使用具有相同视图的不同视图模型
- c# - 如何使用泛型类型的类型参数作为方法参数的类型?