[英]Implementing UITextFieldDelegate with Swift
I have my ViewController class which implements UITextFieldDelegate.我有实现 UITextFieldDelegate 的 ViewController 类。 I have no auto complete for the funcs such as textFieldShouldBeginEditing.
我没有自动完成诸如 textFieldShouldBeginEditing 之类的功能。 Is this a bug in XCode 6?
这是 XCode 6 中的错误吗? Here's my class implementation.
这是我的类实现。
class ViewController: UIViewController, UITextFieldDelegate
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
}
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;
}
更快一点的是......
@IBOutlet weak var nameTF: UITextField! { didSet { nameTF.delegate = self } }
While using Swift Version 3.1 with the outlets of UITextFields, do mark the changes.将 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;
}
}
Xcode 6 (Beta 1) is not currently supporting autocomplete for non-implemented protocol methods/properties (for Swift). Xcode 6 (Beta 1) 目前不支持非实现协议方法/属性的自动完成(对于 Swift)。
Your best bet is to <CMD> - click
on the protocol that isn't yet fully implemented to see what you're missing.您最好的选择是
<CMD> - click
尚未完全实施的协议以查看您缺少什么。
// MARK:- ---> Textfield Delegates // MARK:- ---> 文本域代表
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;
}
Swift 3斯威夫特 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
}
}
I found a little work-around.我找到了一些解决方法。 Just go to file inspector and set type to Objective-C while you are editing the file.
只需在编辑文件时转到文件检查器并将类型设置为 Objective-C。 Auto-completion presents you Swift options.
自动完成为您提供 Swift 选项。
Just switch the type back to Swift when you build.只需在构建时将类型切换回 Swift。
Swift 4:斯威夫特 4:
@IBOutlet weak var yourNameTextField: UITextField! {
didSet {
yourNameTextField.delegate = self
}
}
extension YourNameViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
switch textField {
case yourNameTextField:
yourNameTextField.resignFirstResponder()
default:
break
}
return true
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
return true
}
}
我花了一个漫长的夜晚试图解决这个问题,问题是我的同事在textFieldShouldBeginEditing
而不是textFieldDidBeginEditing
实现了所有逻辑,有时当我在 TextFields 之间使用第一响应者时,没有调用textFieldShouldBeginEditing
...
在我的情况下,我错误地在 swift 类实现范围之外添加了委托方法,这限制了要调用的委托方法。
I had a semicolon by mistake add to the gesture statement, which was responsible for calling view.endEditing(true) which in turns calls the delegate methods such as textFieldShouldBeginEditing.我错误地将一个分号添加到了手势语句中,它负责调用 view.endEditing(true),后者又调用委托方法,例如 textFieldShouldBeginEditing。 Interesting swift does not show any compile time or run time errors for semicolons added sometimes, After removing the semicolon everything just works fine.
有趣的 swift 没有显示有时添加的分号的任何编译时或运行时错误,删除分号后一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.