michaeln peterson michaeln peterson -3 years ago 134
iOS Question

How to make UIPickerView not to display if delegate data is empty on textfield touch?

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.

enter image description here

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() {

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!

applicationUtils.hideActivityIndicator(uiView: self.view)

Answer Source
// 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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download