Noob在这里。我有一个非常长的函数sliderChange()
在滑块值更改时执行,我想将其移动到它自己的Swift控制器文件CR_SlilderChangeCalc.swift
。我在myGlobalVar
中有变量UIViewController
,我也需要从新函数中访问它。如何访问这些变量?
例如View Controller:
class MyDodgyViewController: UIViewController {
// ... some code here
let myGlobalVar: Float = myCalculatedVar
@IBAction func sliderChange(_ sender: UISlider){
sliderChangeCalc(sender: sender)
}
}
例如:CR_SlilderChangeCalc.swift
func sliderChangeCalc(sender: UISlider) {
// doesn't work
let MyNewVar :Float = MyDodgyViewController.MyGlobalVar
// do some cool stuff
}
答案 0 :(得分:1)
您可以将myGlobalVar作为参数传递给方法sliderChangeCalc,即
func sliderChangeCalc(sender: UISlider, myGlobalVar: Float)
{
// do some cool stuff
}
方法调用:
@IBAction func sliderChange(_ sender: UISlider){
sliderChangeCalc(sender: sender, myGlobalVar: self. myGlobalVar)
}