用Swift实现UITextFieldDelegate

时间:2014-06-11 20:19:24

标签: ios delegates swift

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

class ViewController: UIViewController, UITextFieldDelegate

12 个答案:

答案 0 :(得分:34)

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
}

答案 1 :(得分:19)

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

答案 2 :(得分:18)

更开心的是......

@IBOutlet weak var nameTF: UITextField! { didSet { nameTF.delegate = self } }

答案 3 :(得分:9)

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

答案 4 :(得分:7)

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

您最好的选择是<CMD> - click尚未完全实施的协议,以查看您遗失的内容。

答案 5 :(得分:5)

// MARK: - ---&gt;文本字段代表

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

答案 6 :(得分:3)

我发现了一些解决方法。在编辑文件时,只需转到文件检查器并将类型设置为Objective-C即可。自动完成为您提供Swift选项。

在构建时只需将类型切换回Swift。

答案 7 :(得分:3)

Swift 3

[RawRow,RawCol]=size(raw);
for kk=2:RawRow
    studentFName = raw(kk,1);
    allFName = [studentFName];
    allFName = [allFName;studentFName];
end

DataStruct = struct(raw{1,1},allFirstNames,raw{1,2},allLastNames,raw{1,3},Subject1,....)

答案 8 :(得分:1)

斯威夫特4:

dayHeaderClicked(evn){
    // console.log(evn);
    this.viewDate = evn.day.date; // finally get the clicked date value
  }

答案 9 :(得分:0)

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

答案 10 :(得分:0)

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

答案 11 :(得分:0)

我花了整整一个晚上试图解决此问题,但问题是我的同事在textFieldShouldBeginEditing中实现了所有逻辑,而不是textFieldDidBeginEditing,并且有时在我使用{ TextField之间的第一响应者...