【问题标题】:textfield on touch not working触摸文本字段不起作用
【发布时间】:2017-01-24 12:06:03
【问题描述】:

我正在尝试在 UITextField 上设置 ontouch 侦听器,但它根本不起作用这是我的代码:

class SettingsVC: BaseVC {


    @IBOutlet weak var languageField: SkyFloatingLabelTextField!
    @IBOutlet weak var btnburgerMenu: UIButton!
    @IBOutlet weak var headerLbl: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        languageField.addTarget(self, action: "myTargetFunction:", for: UIControlEvents.touchDown)
    }

    func myTargetFunction(textField: SkyFloatingLabelTextField) {
        print("test")
    }

}

这是我遇到的错误

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[IFO.SettingsVC myTargetFunction:]: unrecognized selector sent to instance 0x7fac8b61ff30'

异常的原因是什么?

【问题讨论】:

  • languageField.addTarget(self, action: #selctor(self.myTargetFunction(_:)), for: UIControlEvents.touchDown)
  • Selector 语法在 Swift 3 中改变了,和这个比较 stackoverflow.com/questions/38829151/… 这个是给 GestureRecoginzer 的,你需要用 UItextField 来改变它

标签: ios swift swift3 uitextfield skyfloatinglabeltextfield


【解决方案1】:

应该是这样的——

languageField.addTarget(self, action: #selector(YourViewController.myTargetFunction(sender:)), for: UIControlEvents.touchDown)

或者像这样使用

languageField.addTarget(self, action: #selector(self.myTargetFunction(_:)), forControlEvents: .touchDown)

然后像这样调用函数

func myTargetFunction(_ textField: SkyFloatingLabelTextField) {
    print("test")
}

【讨论】:

    【解决方案2】:

    .touchDown 实际上没什么用,因为它并不总是触发。每次触摸 UITextField 时,我都需要一种让对话框出现的方法。我改用这个:

    class DateTextField: DefaultTextField, UITextFieldDelegate {
        ...
        override init() {
            super.init()
            self.delegate = self // delegate to handle textFieldShouldBeginEditing below
            inputView = UIView() // disable the input view
        }
    
        func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
            // Code to display my date picker dialog
            return false // return false to disable textfield editing
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-12-08
      • 2015-08-24
      • 2014-05-27
      • 2016-02-28
      • 2013-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多