xcode - 警报弹出当前无法正常工作
问题描述
我正在构建一个图表,其中包含带有 onTap 的条形,它调用一个函数来显示总数。该函数具有以下代码,但不喜欢出现警报,“使用未解析的标识符”出现“错误。
在我的代码顶部,我导入了 UIKit,我的功能如下:
func displayTotals(monthSelected: Int, monthTotalQty: Int, monthTotalCurrency: Int){
let alert = UIAlertController(title: "\(monthSelected) Totals", message: "Quantity \(monthTotal) | Total \(monthTotalCurrency)", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .destructive) { _ in
return
})
present(alert, animated: true)
}
解决方案
似乎您在 UIViewController 之外调用此方法。在这种情况下,您可以将视图控制器添加为 func 的参数。
func displayTotals(rootVC: UIViewController, monthTotal: Int, monthSelected: Int, monthTotalQty: Int, monthTotalCurrency: Int){
let alert = UIAlertController(title: "\(monthSelected) Totals", message: "Quantity \(monthTotal) | Total \(monthTotalCurrency)", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .destructive) { _ in
return
})
rootVC.present(alert, animated: true)
}
或者,您可以静态获取根 ViewController,而无需更改 func 的 le 符号:
func displayTotals( monthTotal: Int, monthSelected: Int, monthTotalQty: Int, monthTotalCurrency: Int){
let alert = UIAlertController(title: "\(monthSelected) Totals", message: "Quantity \(monthTotal) | Total \(monthTotalCurrency)", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .destructive) { _ in
return
})
var rootVC = UIApplication.shared.keyWindow?.rootViewController
rootVC?.present(alert, animated: true)
}
推荐阅读
- c# - 在正则表达式中插入变量
- spring-boot - 如何让 Mono 等到依赖 fetch 方法运行
- c# - UpdatePanel 内的 ASP.NET C# FileUpload 和 Postback
- python - 需要帮助来模拟 xhr 请求
- javascript - ajax post调用后重定向不起作用,是什么失败?
- mysql - MySQL Query 连接三个表并从第二个和第三个表中获取数据
- swift - 在具有动态大小的 UIView 上获得漂亮的圆形边框
- tensorflow - 训练期间马修斯相关系数度量错误?
- python - 为什么我在从不同进程读取文件时出现竞争条件?
- c++ - 了解 C++ - “字符文字”与“字符串文字”