// Declare the outlet to the picker in your storyboard
@IBOutlet var myPicker: UIPickerView!
//...
override func viewDidLoad() {
//...
myPicker.selectRow(row, inComponent: 0, animated: true)
}
@IBOutlet weak var mPicker: UIPickerView!
var items: [String] = ["NoName1","NoName2","NoName3"] // Set through another ViewController
var itemAtDefaultPosition: String? //Set through another ViewController
//..
//..
var defaultRowIndex = find(items,itemAtDefaultPosition!)
if(defaultRowIndex == nil) { defaultRowIndex = 0 }
mPicker.selectRow(defaultRowIndex!, inComponent: 0, animated: false)
let pickerArray = [
["displayValue":"--Select--" as AnyObject,"id":"" as AnyObject],
["displayValue":"Jame" as AnyObject,"id":"100" as AnyObject],
["displayValue":"Enamul" as AnyObject,"id":"201" as AnyObject]
]
5条答案
按热度按时间5sxhfpxr1#
这是我在我的一个应用程序中使用的代码:
显然,可以将
row
和0
替换为您想要的任何值。kkbh8khc2#
osh3o9ms3#
SWIFT 5
函数“SetDefaultValue”将通过查找pickerData(用于填充选择器视图)中的项目位置,将pickerView定位在所需字符串上。在本例中,它将是“项目2”
9rnv2umw4#
尝试将该行
myPicker.selectRow(row, inComponent: 0, animated: true)
放入覆盖函数视图DidAppear中7eumitmz5#
在swift 3的选择器视图中选择默认值
如果你的阵列像风箱
只需为选择视图中选定的默认值添加以下行