【发布时间】:2018-01-24 15:46:57
【问题描述】:
当用户在文本字段内触摸以开始编辑时,如果 statusArray 为空,我不想显示 UIPickerView。我的要求是在输入 3 个字符后,我要进行 Web 服务调用。在主应用程序中,我收到了所有响应数据并显示在 UIPickerView 上。请建议我在收到服务器响应之前如何隐藏 UIPickerView。
这是我的示例代码(不包括 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)
}
【问题讨论】:
-
api调用有回调方法吗?
-
是的,请检查我上面的代码。
-
然后你在回调方法中显示显示选择器视图并始终隐藏
-
@SaurabhJain,我尝试在 Web 服务回调调用中添加此代码,但它没有打开任何东西。 //绑定文本框到选择器视图 self.txtLocationCode.inputView = self.picker
-
谢谢大家
标签: ios swift3 uitextfield uipickerview