我有3个文字字段,喜欢选择性别,国家,州。我使用UIPicker
来获取UIPicker
的输入值。但是现在当我按性别UITextField
打开UIPicker enter code here
时,UIPicker enter code here
显示没有数据。但同样的UIPicker
我用于国家,州。那个时候,它显示了两个国家/地区的UIPicker
数据,状态文本字段。任何人都可以帮助我。如何解决这个问题。
错误:
致命错误:索引超出范围
如果我在uipicker选项视图中按下它的崩溃。
我的代码:
我在这个类文件中崩溃了:
import Foundation
import SwiftCountryPicker
class DataPicker: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {
var dataPicker: UIPickerView!
var inputText:UITextField!
var parent:UIViewController!
var pickerData: [String]!
var feetStr = String()
var inchStr = String()
private var _selectedValue: String!
var selectedvalue: String {
return _selectedValue ?? String()
}
func showPicker(parent:UIViewController,inputText:UITextField, data: [String], selectedValueIndex: Int){
self.inputText = inputText
self.parent = parent
pickerData = data
if dataPicker == nil {
dataPicker = UIPickerView(frame: CGRectMake(0,0,parent.view.frame.size.width, 216))
dataPicker.backgroundColor = UIColor.whiteColor()
dataPicker.dataSource = self
dataPicker.delegate = self
dataPicker.selectRow(selectedValueIndex, inComponent: 0, animated: true)
_selectedValue = pickerData.count > 0 ? pickerData[selectedValueIndex] : ""
}
inputText.inputView = dataPicker
// ToolBar
let toolBar = UIToolbar()
toolBar.barStyle = .Default
toolBar.translucent = true
toolBar.tintColor = UIColor(hex: "B12420")
toolBar.backgroundColor = UIColor.whiteColor()
toolBar.sizeToFit()
// Adding Button ToolBar
let doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: #selector(DataPicker.doneClick))
let spaceButton = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: #selector(DataPicker.cancelClick))
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
inputText.inputAccessoryView = toolBar
}
func doneClick() {
inputText.text = _selectedValue
inputText.resignFirstResponder()
}
func cancelClick() {
inputText.resignFirstResponder()
}
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]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
_selectedValue = pickerData[row]
}
}
在这一行:
func pickerView(pickerView:UIPickerView,didSelectRow row:Int, inComponent组件:Int){
_selectedValue = pickerData[row] }
我的文本字段代表按下texfield:
extension UserInfoViewController:UITextFieldDelegate{
func textFieldDidBeginEditing(textField: UITextField) {
selectedText = textField
else if textField == genderss {
ctDataPicker = DataPicker()
let indexPos = genders.indexOf(genderss.text!) ?? 0
ctDataPicker.showPicker(self, inputText: textField, data: genders,selectedValueIndex: indexPos)
}
}
}
答案 0 :(得分:1)
UIPickerViewDataSource
方法didSelectRow
在Swift 4中已更改:
func numberOfComponents(in 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]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
TextField.text = myPickerData[row]
}
尝试将使_selectedValue崩溃的行更改为文本字段示例
TextField.text = myPickerData [行]
在UIPickerView
上检查Apple Documentation。