首页 > 解决方案 > 如何快速从另一个控制器调用函数

问题描述

我在 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。

https://imgur.com/fg2502P

https://imgur.com/C4AzaRY

https://imgur.com/jOrwujy

标签: swiftfunctionuiviewcontrollerdidselectrowatindexpath

解决方案


如果您想调用从 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,在此处创建您想要在整个应用程序中使用的所有函数。它们成为“全局函数”


推荐阅读