0

我尝试使用这种方法selectedRow(inComponent: 0),但它对我不起作用。有什么办法可以在不活动的情况下从Picker中获取第一个/默认值?

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
    }
}
4

1 回答 1

0

没有大碍-

只需将您的选择器视图定义为全局并访问objectAtIndex(0)

并将这行代码写在viewDidLoad()

不准确,但类似这样——

@IBOutlet var yourPicker: UIPickerView! = UIPickerView()

var yourArray = ["Apple", "Samsung", "Nokia"]


override func viewDidLoad() {
    super.viewDidLoad()
    yourPicker.text = yourArray[0]
}
于 2017-07-10T05:10:18.110 回答