用Swift实现UITextFieldDelegate

Bea*_*wah 22 delegates ios swift

我有我的ViewController类,它实现了UITextFieldDelegate.我没有像textFieldShouldBeginEditing这样的func自动完成.这是XCode 6中的错误吗?这是我的类实现.

class ViewController: UIViewController, UITextFieldDelegate
Run Code Online (Sandbox Code Playgroud)

jay*_*iya 39

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
}
Run Code Online (Sandbox Code Playgroud)


Kir*_*n K 22

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;
    }
Run Code Online (Sandbox Code Playgroud)


Dog*_*fee 18

更开心的是......

@IBOutlet weak var nameTF: UITextField! { didSet { nameTF.delegate = self } }
Run Code Online (Sandbox Code Playgroud)


Piy*_*hur 10

使用Swift Version 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;
    }
}
Run Code Online (Sandbox Code Playgroud)


Jia*_*aro 7

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

您最好的选择是使用<CMD> - click尚未完全实现的协议来查看您缺少的内容.


小智 7

// MARK: - ---> Textfield代表

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;
}
Run Code Online (Sandbox Code Playgroud)