【发布时间】:2019-10-18 14:10:36
【问题描述】:
我在 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()
}
DetailViewController:
@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。
【问题讨论】:
-
您需要做的就是从您的 DetailViewController 的 viewDidLoad 调用 getChartData()。
标签: swift function uiviewcontroller didselectrowatindexpath