這是一個令我困擾的超級基本問題。Swift - 在另一個函數中使用來自UISlider IBAction的值
我有一個UIslider IBAction生成一個Double(var = rounded)。我想在viewDidLoad中使用這個double。但得到的錯誤 「使用未解決的標識符的 '四捨五入'」
@IBAction func sliderValueChanged(sender: UISlider) {
var currentValue = Double(sender.value)
var rounded = Double(round(100*currentValue)/100)
label.text = "\(rounded)"
}
override func viewDidLoad() {
super.viewDidLoad()
let fileURL = NSBundle.mainBundle().URLForResource("puppy", withExtension: "jpg")
let beginImage = CIImage(contentsOfURL: fileURL)
let filter = CIFilter(name: "CISepiaTone")
filter.setValue(beginImage, forKey: kCIInputImageKey)
filter.setValue(rounded, forKey: kCIInputIntensityKey)
let newImage = UIImage(CIImage: filter.outputImage)
self.imageView.image = newImage
}
filter.setValue(四捨五入,forKey:kCIInputIntensityKey)是我在哪裏得到的錯誤。我想在這裏使用slider函數中的'rounded'變量。
任何幫助在一個函數中使用一個變量在另一個函數將非常讚賞。我偶然遇到過這樣的情況,但沒有成功。所以一旦你們都在這裏幫助我,它也應該解決我的其他問題。
由於
我認爲你缺乏基本概念:i)圓角變量不是類變量,因此只能在定義它的函數中訪問。 ii)即使它是一個類變量,在viewDidLoad中也不會使用該變量,因爲在設置了round的值之前調用了該函數。 – Shoaib
感謝您的輸入!實際上缺乏基本概念。但仍然在學習。所以我會最終得到它:-) - 你將如何修復我的代碼,以實現我期待的結果? – Joe