簡體   English   中英

“使用未解析的標識符” SWIFT

[英]“use of unresolved identifier” SWIFT

錯誤

使用未解析的標識符“滑塊”

到哪里說

var slideValue = slider.value

有什么問題??

 import UIKit

    class ViewController: UIViewController {

        @IBAction func colorChanger(sender: UISlider) {
            // creates variable to hold new color
            var newBackgroundColor : UIColor

            // creates variable holding the value from slider
            var sliderValue = slider.value

            // changes the newBackgroundColor variable to new color values.
            newBackgroundColor = UIColor(hue: sliderValue, saturation: 0.5, brightness: 0.5, alpha: 0.5)

            // changes the background color
            self.view.backgroundColor = newBackgroundColor    }

就我(或Swift ;-)而言,您在代碼中沒有定義slider屬性。

相反,您的方法中有一個名為sender的參數,它是一個UISlider因此請嘗試使用該參數。

var sliderValue = sender.value

要么

sender重命名為slider

因此,這是一種方法(在@ leo-dabus的幫助下)。 我也很自由地清理了您的代碼,對不起:-)

import UIKit

class ViewController: UIViewController {

@IBAction func colorChanger(sender: UISlider) {
    // creates variable holding the value from slider
    let sliderValue = CGFloat(sender.value)

    // changes the newBackgroundColor variable to new color values.
    let newBackgroundColor = UIColor(hue: sliderValue, saturation: 0.5, brightness: 0.5, alpha: 0.5)

    // changes the background color
    view.backgroundColor = newBackgroundColor    
}

我為您的問題找到了解決方案

首先掛上滑塊

@IBOutlet var slider: UISlider!

然后在情節提要或xib中或通過編程設置所需的最小值,最大值和當前值

let sliderValue = Int(slider.value)
print("Slider value is now - \(sliderValue)")

輸出量

Slider value is now - 6

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM