如果在文本字段触摸时委托数据为空,如何使 UIPickerView 不显示?
How to make UIPickerView not to display if delegate data is empty on textfield touch?
如果用户在文本字段内触摸以开始编辑时 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)
}
也许你可以在 viewDidLoad
中这样制作:
if statusArray.count == 0 {
picker.isHidden = true
}
在网络服务响应中:
if statusArray.count > 0{
statusText.inputView = picker
}
首先在viewDidLoad
方法中检查statusArray.count
。
if statusArray.count == 0 {
picker.isHidden = true
}
插入3个字符后向服务器请求获取数据。(按照你说的。)
并检查以下条件 ...
if statusArray.count > 0 {
picker.isHidden = false
}
// 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()
}
只需在任意位置编写并调用 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)
}
}
如果用户在文本字段内触摸以开始编辑时 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)
}
也许你可以在 viewDidLoad
中这样制作:
if statusArray.count == 0 {
picker.isHidden = true
}
在网络服务响应中:
if statusArray.count > 0{
statusText.inputView = picker
}
首先在viewDidLoad
方法中检查statusArray.count
。
if statusArray.count == 0 {
picker.isHidden = true
}
插入3个字符后向服务器请求获取数据。(按照你说的。)
并检查以下条件 ...
if statusArray.count > 0 {
picker.isHidden = false
}
// 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()
}
只需在任意位置编写并调用 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)
}
}