如果在textfield touch上委托数据为空,如何使UIPickerView不显示?

时间:2017-08-16 13:13:25

标签: ios swift3 uitextfield uipickerview

当用户在文本字段内部进行修改以进行开始编辑时,如果statusArray为空,我不想显示UIPickerView。我的要求是在输入3个字符后,我将进行Web服务调用。在主应用程序中,我收到了所有响应数据&在UIPickerView上显示。请建议我如何隐藏UIPickerView,直到我收到服务器的响应。

enter image description here

这是我的示例代码(不包括Web服务调用)

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

}

//来自网络服务的回调方法

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

5 个答案:

答案 0 :(得分:2)

// 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()
}

答案 1 :(得分:0)

也许你可以在viewDidLoad中这样做:

if statusArray.count == 0 {
    picker.isHidden = true
}

答案 2 :(得分:0)

在网络服务响应中:

if statusArray.count > 0{
  statusText.inputView = picker
}

答案 3 :(得分:0)

首先检查statusArray.count方法中的viewDidLoad

if statusArray.count == 0 {
    picker.isHidden = true
}

插入3个字符后,请求服务器获取数据。(按照你说的那样。)
并检查以下条件......

if statusArray.count > 0 {
    picker.isHidden = false
}

答案 4 :(得分:0)

只需在任意位置的pickerview扩展中编写并调用该函数,就会重置到picker视图的第一行

extension UIPickerView {

    func reloadPicker(_ animated : Bool){
        print("\n\n Extension Is Calling \n\n")
        self.reloadAllComponents()
        self.selectRow(0, inComponent: 0, animated: animated)
    }
}