Cha*_*Guy 2 string global-variables uipickerview xcode7 swift2
当用户从 中选择一个状态时 UIPickerView
,它会将它们带到不同的viewController
带有所选状态的数据。当他们点击“返回”按钮,进入到viewController
了pickerView
它总是起始于与第一状态的顶部,在这种情况下,美国亚利桑那州。我怎样才能让它已经滚动到中间并选择田纳西州,例如,如果那是他们最初选择的州?我正在使用 Xcode 7 和 Swift 2,谢谢!
我有一个UIPickerView
从以下位置获取数据的数据:
let pickerData = ["Arizona", "Arkansas", "California","Tennessee", "Texas", "Utah", "Virginia"]
Run Code Online (Sandbox Code Playgroud)
当用户在 中选择一个状态时,pickerView
它会获取该值并将其添加到变量中:
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//Takes the selected state and adds it to the variable 'currentState'
currentState = pickerData[row]
savedStateGV = currentState
}
Run Code Online (Sandbox Code Playgroud)
“savedStateGV”是全局变量,“currentState”是局部变量。两者都是String
类型。
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
Run Code Online (Sandbox Code Playgroud)
您可以在视图控制器中定义一个属性 middleOfPicker(一个整数),并且您可以使用以下代码序列在例程“viewDidLoad”(您的选择器是您给选择器的 IBOutlet 名称)中设置此属性:
middleOfPicker = pickerData.count / 2
yourpicker.selectRow = middleOfPicker
Run Code Online (Sandbox Code Playgroud)
最后,“后退按钮”的动作例程应该包括以下语句:
yourpicker.selectRow = middleOfPicker
Run Code Online (Sandbox Code Playgroud)
在迅捷4中
yourpicker.selectRow(middleOfPicker, inComponent: 0, animated: true)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2560 次 |
最近记录: |