如何标记扩展文件中包含的已编程文本字段(swift4)

时间:2018-08-02 04:29:01

标签: ios tags swift4 extension-methods textfield

我想同时标记文本字段ana和sampleTextfield。这样,在扩展字段中,我可以做一些事情,例如为每个textfireld调用不同的pickerview。示例:

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

代码:

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

0 个答案:

没有答案