I do not want to display UIPickerView if statusArray is empty when user touch up inside the text field for start editing. My requirement is after entering 3 characters, I'm going to make a web service call. In main app, I received all the response data & display on UIPickerView. Please suggest me how to hide UIPickerView till I received response from server.
Here is my sample code (web service call not included)
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var statusText: UITextField!
let statusArray = [String]()
let picker = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
picker.dataSource = self
picker.delegate = self
//binding textfield to picker view
statusText.inputView = picker
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return statusArray.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return statusArray[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
statusText.text = statusArray[row]
}
}
//callback method from web service
DispatchQueue.main.sync {
for location in locationDetails.ianaLocationInfoArray
{
let optionToView = location.ianaCode! + " - " + location.facilityName!
self.locationArray.append(optionToView)
self.picker.reloadAllComponents()
}
applicationUtils.hideActivityIndicator(uiView: self.view)
}
// set inputView later if condition meets
// txtLocationCode.inputView = picker
// In callback method from web service, change the input type
// to picker instead of keyboard if condition meets
if(self.locationArray.count>0) {
// below 3 lines are to set inputview as picker and to see picker immediately
self.txtLocationCode.inputView = self.picker
self.txtLocationCode.resignFirstResponder()
self.txtLocationCode.becomeFirstResponder()
}