2015-10-15 45 views
0

我是一個快速相信我的新手我已經搜索並搜索了答案。我想創建UISliders,從Swift中的數組中獲取它們的值。它是一款相機應用程序,因此示例數組應該很明顯。Swift UISlider從數字數組中獲取它的值

@IBAction func isoValueChanged(sender: UISlider) { 
    let isoArray = ["24", "32", "50","64","80","100","125","160","200","250","320","400","500","640","720","800","1000","1250","1600","1800"] // available iPhone 6s ISO settings I believe 
    let currentValue = // What do I need? 
    isoText.text = "\(currentValue)" 
} 

即使更難代表快門速度從1/1000 - 32! 從我看到的情況來看,這不是一件容易的事情,因爲沒有數學表示來從數組中計算。它甚至有可能嗎?

回答

1

我不太確定我明白你想要什麼,但我猜這是對的。

// Global Variable or something like this (accessible from multiple functions) 
let isoArray = ["24", "32", "50","64","80","100","125","160","200","250","320","400","500","640","720","800","1000","1250","1600","1800"] // available iPhone 6s ISO settings I believe 

func functionThatCreatesTheSliderYo(...) { 
    slider.minimumValue = 0 
    slider.maximumValue = isoArray.count 
    slider.continuous = false 
} 

@IBAction func isoValueChanged(sender: UISlider) { 
    // instead of Int(val) you may want to round(val) for a better UI 
    let currentValue = isoArray[Int(sender.value)] 
    isoText.text = "\(currentValue)" 
} 
+0

謝謝。我明白爲什麼數組變成全局的,但我不太明白「func functionThatCreatesTheSliderYo(...)」@IBOutlet弱var isoSlider:UISlider!對不起,在學習階段。 – user3118171

+0

沒錯。區別在於網點是通過故事板創建的,UISliders是以編程方式創建的。 Outlets是UISlider對象,只是爲了XCode Storyboard界面而進行了很多改動。你應該能夠設置'outlet.minimumValue = 0'(或'(outlet as!UISlider).minimumValue = 0'。如果所有這些都不起作用,你應該可以在故事板上設置'.minimumValue'' 。attributes'部分 –

+0

FUNC isoSlider(發件人:UISlider){ isoSlider.minimumValue = 0 isoSlider.maximumValue = isoArray.count isoSlider.continuous =假 }不行的,但是這是我的第一個想法喔,圓拋出錯誤 – user3118171

0

這對我的作品

@IBAction func isoValueChanged(sender: UISlider) { 
    let isoArray = ["24", "32", "50","64","80","100","125","160","200","250","320","400","500","640","720","800","1000","1250","1600","1800"] 
    let currentValue = isoArray[Int(sender.value)] 
    isoText.text = "\(currentValue)" 

} 

那麼簡單,因爲它應該是斯威夫特。非常感謝Ty。一定要看到聊天。