如何在 PickerView 首次弹出时/用户尚未开始任何访问时从中获取第一个值
How to get the very first value from PickerView when it first pop up / when user not yet start any access
我尝试使用这种方法selectedRow(inComponent: 0)
,但它对我不起作用。有什么办法可以在选择器不活动时从选择器中获取 first/default 值?
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return countries.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return countries[row].name
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
country.text = countries[row].name //`country.text` is just textField
}
}
没什么大不了的-
只需将您的选择器视图定义为全局视图并访问 objectAtIndex(0)
的值
并将这行代码写在viewDidLoad()
不准确,大概是这样-
@IBOutlet var yourPicker: UIPickerView! = UIPickerView()
var yourArray = ["Apple", "Samsung", "Nokia"]
override func viewDidLoad() {
super.viewDidLoad()
yourPicker.text = yourArray[0]
}
我尝试使用这种方法selectedRow(inComponent: 0)
,但它对我不起作用。有什么办法可以在选择器不活动时从选择器中获取 first/default 值?
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return countries.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return countries[row].name
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
country.text = countries[row].name //`country.text` is just textField
}
}
没什么大不了的-
只需将您的选择器视图定义为全局视图并访问 objectAtIndex(0)
并将这行代码写在viewDidLoad()
不准确,大概是这样-
@IBOutlet var yourPicker: UIPickerView! = UIPickerView()
var yourArray = ["Apple", "Samsung", "Nokia"]
override func viewDidLoad() {
super.viewDidLoad()
yourPicker.text = yourArray[0]
}