swift - 如何快速从另一个控制器调用函数
问题描述
我在 DetailView Controller 上设置了 Show Charts 按钮,它触发 getChartData 函数并在图表的显示视图中显示值,现在我想在主 Viewcontroller 的 didselectrow 中调用该函数,以便自动加载图表,但它失败。当我尝试在 didselectrow (DVC.getChartsData) 中调用该函数时,出现错误“线程 1:致命错误:在隐式展开可选值时意外发现 nil”
DVC.getChartsData
线程 1:致命错误:在隐式展开可选值时意外发现 nil
视图控制器:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let Storyboard = UIStoryboard(name: "Main", bundle: nil)
let DVC = Storyboard.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
DVC.getDetailName = coin[indexPath.row].name
let formatedRoundingPrice = (coin[indexPath.row].price as NSString).floatValue * currencymodel.indexValue
let formatedPrice = String (format: "%.3f", formatedRoundingPrice)
DVC.getDetailPrice = formatedPrice
self.navigationController?.pushViewController(DVC, animated: true)
let percentage = String ((coin[indexPath.row].percent as NSString).floatValue)
DVC.getDetailPercent = percentage
tableView.deselectRow(at: indexPath, animated: true)
//DVC.getChartData()
}
细节视图控制器:
@IBAction func tapLineChart(_ sender: Any) {
getChartData()
}
func getChartData () {
let chart = HITLineChartView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: displayView.bounds.height))
displayView.addSubview(chart)
let max = String((priceResult.max() ?? 0.0).rounded(.up))
let min = String((priceResult.min() ?? 0.0).rounded(.down))
let maxChange = abs((listOfChanges.max()) ?? 0.0).rounded(.up)
let minChange = abs((listOfChanges.min()) ?? 0.0).rounded(.up)
absMaxPercentage = Int(maxChange > minChange ? maxChange : minChange)
titles = ["\(getDetailName) closing price is \(getDetailPrice)"]
print(data)
chart.draw(absMaxPercentage,
values: listOfChanges,
label: (max: max, center: "", min: min),
dates: namesArray,
titles: titles)
addCloseEvent(chart)
finalURL = baseURL + "bitcoin" + "/market_chart?vs_currency=usd&days=5"
print(finalURL)
getBitcoinData(url: finalURL)
}
如何加载我的图表点击特定的表格视图单元格而不是点击 TapLineChart。
解决方案
如果您想调用从 viewController A 声明的 viewControllerB 上的函数。
只需创建要使用函数的类文件的对象
var obj mainVC = MainViewController()
class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func commonMethod() {
print("From the main class")
}
}
使用该对象,在您打算使用它的另一个文件中调用该函数
class OtherViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
mainVC.commonMethod()
// Do any additional setup after loading the view, typically from a nib.
}
}
此外,您还可以创建一个新的 swift 文件,将其命名为 Global.swift,在此处创建您想要在整个应用程序中使用的所有函数。它们成为“全局函数”
推荐阅读
- python - 理解 exe/dll 文件的编码
- bluetooth - 如何找到我的 Mulitmeter 的服务 UUID
- c++ - 输入 gpio 意外触发中断
- python - 使用涉及 shift() 的其他列的最大值创建一个新的 df 列
- prolog - 使用 prolog 的文件问题(存在错误)
- java - 我正在尝试从 abc 公司地址向 gmail 或任何其他邮件服务器发送电子邮件,但连接超时
- linux - 如何在 centos 8 上安装 SSMTP 并在 Zabbix 上设置电子邮件通知?
- firebase - Firebase Firestore 有条件地按 2 个字段排序
- windows - tcl:如何在 Windows 上将 UTF-8 文本打印到标准输出
- javascript - @neo4j/graphql:auth JWT 未配置