How to tag programmed textfields featured in a extension file (swift4)

I want to tag both textfield ana and sampleTextfield. So that in the extension field I can do things like call different pickerviews for each textfireld. Example:

            func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if tag ==1 {
    return firstHit[row]
} else {
    return sendHit[row]
} }

Code:

            var ana =  UITextField(frame: CGRect(x: -20, y: 100, width: 300, height: 40))
     var sampleTextField =  UITextField(frame: CGRect(x: 20, y: 100, width: 300, height: 40))

   extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {
  func numberOfComponents(in pickerView: UIPickerView) -> Int {
   return 1
   }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
      return firstHit.count
   }

   func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
   return firstHit[row]
 }

 func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
   selecteDay = firstHit[row]
   sampleTextField.text = selecteDay
}

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
   var lable: UILabel
   if let view = view as? UILabel {
       lable = view
   } else {
       lable = UILabel()
   }

   lable.textColor = .white
   lable.textAlignment = .center
   lable.font = UIFont(name: "Menlo-Regular", size: 17)
   lable.text = firstHit[row]
   return lable
 }}

Hello @timswift

You can assign tag to UITextField, but we cannot identify them in PickerView Delegate.

So you need to do this way to identify your textfield and load data :

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
      if ana.isFirstResponder {
          return firstHit.count
    }
    else if sampleTextField.isFirstResponder {
        return sendHit.count
    }
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if ana.isFirstResponder {
        return firstHit[row]
    }
    else if sampleTextField.isFirstResponder {
        return sendHit[row] 
    }
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if ana.isFirstResponder {
        selecteDay = firstHit[row]
        sampleTextField.text = selecteDay
    }
    else if sampleTextField.isFirstResponder {
          // Do something
    }
}

@vimipatel do I put this code in the extension file.

Hi @timswift,
As a rule of thumb, extensions do not have specific items. So if you have a label or a textfield then it would not go into an extension of a class that you do not own or have access to. In this case, the textfield belongs to the viewcontroller but you would have a UIPickerView extension.

Unless you mean something else when you say extension.

cheers,

Jayant

Yeah, sure… Put this code and try.

This topic was automatically closed after 166 days. New replies are no longer allowed.