用Swift实现UITextFieldDelegate

我有我的ViewController类实现UITextFieldDelegate。 我没有自动完成的func,如textFieldShouldBeginEditing。 这是XCode 6中的错误吗? 这是我的类实现。

class ViewController: UIViewController, UITextFieldDelegate 

Xcode 6(Beta 1)当前不支持未实现的协议方法/属性(Swift)的自动完成。

您最好的select是<CMD> - click尚未完全实施的协议,查看您缺less的内容。

 class ViewController: UIViewController,UITextFieldDelegate //set delegate to class @IBOutlet var txtValue: UITextField //create a textfile variable override func viewDidLoad() { super.viewDidLoad() txtValue.delegate = self //set delegate to textfile } func textFieldDidBeginEditing(textField: UITextField!) { //delegate method } func textFieldShouldEndEditing(textField: UITextField!) -> Bool { //delegate method return false } func textFieldShouldReturn(textField: UITextField!) -> Bool { //delegate method textField.resignFirstResponder() return true } 

更快一点是…

 @IBOutlet weak var nameTF: UITextField! { didSet { nameTF.delegate = self } } 
 Swift 3.0.1 // UITextField Delegates func textFieldDidBeginEditing(_ textField: UITextField) { } func textFieldDidEndEditing(_ textField: UITextField) { } func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { return true; } func textFieldShouldClear(_ textField: UITextField) -> Bool { return true; } func textFieldShouldEndEditing(_ textField: UITextField) -> Bool { return true; } func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { return true; } func textFieldShouldReturn(_ textField: UITextField) -> Bool { textField.resignFirstResponder(); return true; } 

使用Swift版本3.1与UITextFields的出口,做标记更改。

 import UIKit class LoginViewController: UIViewController, UITextFieldDelegate { @IBOutlet var txtUserID: UITextField! @IBOutlet var txtPwd: UITextField! override func viewDidLoad() { super.viewDidLoad() txtUserID.delegate = self txtPwd.delegate = self } // UITextField Delegates func textFieldDidBeginEditing(_ textField: UITextField) { print("TextField did begin editing method called") } func textFieldDidEndEditing(_ textField: UITextField) { print("TextField did end editing method called\(textField.text!)") } func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { print("TextField should begin editing method called") return true; } func textFieldShouldClear(_ textField: UITextField) -> Bool { print("TextField should clear method called") return true; } func textFieldShouldEndEditing(_ textField: UITextField) -> Bool { print("TextField should end editing method called") return true; } func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { print("While entering the characters this method gets called") return true; } func textFieldShouldReturn(_ textField: UITextField) -> Bool { print("TextField should return method called") textField.resignFirstResponder(); return true; } } 

// MARK: – >> Textfield Delegates

 func textFieldDidBeginEditing(textField: UITextField) { print("TextField did begin editing method called") } func textFieldDidEndEditing(textField: UITextField) { print("TextField did end editing method called\(textField.text)") } func textFieldShouldBeginEditing(textField: UITextField) -> Bool { print("TextField should begin editing method called") return true; } func textFieldShouldClear(textField: UITextField) -> Bool { print("TextField should clear method called") return true; } func textFieldShouldEndEditing(textField: UITextField) -> Bool { print("TextField should end editing method called") return true; } func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { print("While entering the characters this method gets called") return true; } func textFieldShouldReturn(textField: UITextField) -> Bool { print("TextField should return method called") textField.resignFirstResponder(); return true; } 

我find了一点办法。 只要进入文件检查器,并在编辑文件时将types设置为Objective-C。 自动完成将呈现Swift选项。

只需在构build时将types切换回Swift。

Swift 3

  @IBOutlet weak var yourNameTextfield: UITextField! { didSet { yourNameTextfield.delegate = self } } extension YourNameViewController: UITextFieldDelegate { func textFieldDidBeginEditing(_ textField: UITextField) { } func textFieldDidEndEditing(_ textField: UITextField) { } func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { return true } func textFieldShouldClear(_ textField: UITextField) -> Bool { return true } func textFieldShouldEndEditing(_ textField: UITextField) -> Bool { return true } func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { return true } func textFieldShouldReturn(_ textField: UITextField) -> Bool { textField.resignFirstResponder(); return true } } 

在我的例子中,我错误地添加了委托方法在swift类的实现范围之外,并限制了委托方法被调用。

我有一个错误的分号添加到手势语句,它负责调用view.endEditing(true),然后调用委托方法,如textFieldShouldBeginEditing。 有趣的swift不会显示有时添加分号的任何编译时间或运行时错误,删除分号后,一切工作正常。