-1
如何將UIPickerView
選擇存儲到變量中?將UIPickerView選擇存儲到變量中
func pickerView(pickerView: AKPickerView, didSelectItem item: Int) {
}
let selected = pickerView(samplePickerView, what goes here?)
如何將UIPickerView
選擇存儲到變量中?將UIPickerView選擇存儲到變量中
func pickerView(pickerView: AKPickerView, didSelectItem item: Int) {
}
let selected = pickerView(samplePickerView, what goes here?)
首先你會拿着一個數組我猜。所以讓我們將它命名爲pickerarray,並將它作爲一個字符串數組。
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int){
selected = pickerarray[row] // selected is the variable
}
這裏您使用的是pickerview的row屬性。
編輯 爲了您的澄清,我進入整個代碼
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource,UIPickerViewDelegate {
@IBOutlet var MyPickerView: UIPickerView!
var selected = ""
let words: NSArray = ["Cat", "Chicken", "fish", "Dog",
"Mouse", "Guinea Pig", "monkey"]
override func viewDidLoad() {
super.viewDidLoad()
MyPickerView.delegate = self
MyPickerView.dataSource = self
selected = words[0] // setting the default value
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return words.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return "\(words[row])"
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selected = words[row] as! String
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
從pickerview從任何函數訪問選定的值,使用
let selectedvalue = selected
但說我要訪問的在另一個函數中選擇的變量,並且必須使用:let selected = pickerView(samplePickerView,________)我將傳遞給函數以獲取選定的行。(如果我傳遞任何Int,它只會返回索引int) – user3773060
@ user3773060我已經在上面解釋過了。請看看。 –