【问题标题】:How to make UIPickerView not to display if delegate data is empty on textfield touch?如果文本字段触摸时委托数据为空,如何使 UIPickerView 不显示?
【发布时间】: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


【解决方案1】:

只需在pickerview扩展中的任何地方编写和调用函数,然后将选择器视图重置到第一行

extension UIPickerView {

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

【讨论】:

    【解决方案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()
    }
    

    【讨论】:

    • picker.isHidden = true/false 在代码中不起作用。我尝试了很多时间来隐藏我的 UIPickerView,即使在没有任何条件的 viewDidLoad 方法中它仍然没有隐藏这个元素。
    • 目前您的选择器仅在您点击 statusText 时显示?
    • 你能检查一下picker是否为nil吗?也请查看link
    • 是的,它一直在显示。我想在输入 3 个字符后打开它。所以我的网络服务可以完成请求并为我准备好 UIPickerView 数据。
    • 非常感谢您为解决我的问题付出了巨大的努力。高度赞赏。
    【解决方案3】:

    在网络服务响应中:

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

    【讨论】:

      【解决方案4】:

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

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

      插入 3 个字符后,向服务器发出请求以获取数据。(如您所说。)
      并检查以下情况...

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

      【讨论】:

        【解决方案5】:

        也许你可以在viewDidLoad

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

        【讨论】:

        • 它不工作。它仍然向我显示选择器视图。我试过 .isHidden 和 .alpha 但没有运气。
        • @michaelnpeterson,附加选择器以查看的代码在哪里?
        • statusText.inputView = 选择器
        • @michaelnpeterson,如果上面写了,你可以用委托换行
        猜你喜欢
        • 2023-04-01
        • 2022-01-19
        • 1970-01-01
        • 1970-01-01
        • 2018-09-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-22
        • 1970-01-01
        相关资源
        最近更新 更多