I am using following code for the picker function in a UITextfield, how should I ensure that the value from the array is chosen and no other data input is there I shared the code below, I used the "guard" to check whether field is empty or not Code
var spinneCPickerData: [String] = ["1", "2", "3", "4", "5"]
@IBOutlet var pickerfield: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let spinnerCPicker = UIPickerView()
spinnerCPicker.delegate = self
pickerfield = spinnerCPicker
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView( _ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return spinneCPickerData.count
}
func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return spinneCPickerData[row]
}
func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerfield.text = spinneCPickerData[row] as String
view.endEditing(true)
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// This method is triggered whenever the user makes a change to the picker selection.
// The parameter named row and component represents what was selected.
}
func setdata() {
guard let text data = PostCategoryField.text, !postcategory.isEmpty, else {
self.view.makeToast("Please Enter the Category!")
return
}
}