1
votes

I have 3 text field like to select gender, country, state. And I am using UIPicker to get the input values from UIPicker. But now when I press gender UITextField to open UIPickerenter code here, the UIPickerenter code here is showing with no data. But the same UIPicker I am using for the country, state. that time it's showing the data with UIPicker for both countries, state text field. Can anyone please help me out. How to solve this problem.

Error :

fatal error: Index out of range

If I press in uipicker option view only its crashing.

My code :

I am getting a crash in this class file :

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

}

In this line :

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        _selectedValue = pickerData[row]
    }

And my text field delegate for press the 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)
        }

}
}
1
first check let indexPos = genders.indexOf(genderss.text!) ?? 0 indexPos value and 'pickerData' count is samePiyushRathi
once check pickerData it has any data or not?Andey Satyanarayana
what value contains genders array?jignesh Vadadoriya
Technically that does not happen since you populated the picker with the same Array. You might want to check if the array mutated between populating the picker and selecting a row.Ben Ong
@AndeySatyanarayana Satyanarayana The number is 0 (indexpos) The ndata is 0 (pickerData.count)mack

1 Answers

1
votes

UIPickerViewDataSource method didSelectRow is changed in 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]
}

Try changing the line crashing _selectedValue to a textfield example

TextField.text = myPickerData[row]

Check Apple Documentation on UIPickerView.