本文介绍了Swift可重用的UITextFieldDelegate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试实现可重复使用的 UITextFieldDelegate
类,如下所示:
I am trying to implement a reusable UITextFieldDelegate
class as follows:
class CustomTextFieldDelegate : NSObject, UITextFieldDelegate
所有委托协议方法均已正确实现。
All delegate protocol methods are implemented correctly.
在控制器
中,将 delegate
分配给 UITextField
textField.delegate = CustomTextFieldDelegate()
问题是没有调用任何委托函数。但是,当我从控制器实现委托协议时,一切正常
The problem is that none of the delegate functions get called. However, when I implement the delegate protocol from the controller, then things work fine
class CustomTableViewController: UITableViewController, UITextFieldDelegate
有什么想法吗?
推荐答案
如果要在整个项目中重用CustomTextFieldDelegate,则应使用Singleton实例;
If your want to reuse CustomTextFieldDelegate through out the project, you should use a Singleton instance;
textField.delegate = CustomTextFieldDelegate.sharedInstance
和类更改
class CustomTextFieldDelegate : NSObject, UITextFieldDelegate {
static let sharedInstance:CustomTextFieldDelegate = CustomTextFieldDelegate();
func textFieldDidBeginEditing(textField: UITextField) {
NSLog("textFieldDidBeginEditing ...");
}
//... other methods
} //F.E.
这篇关于Swift可重用的UITextFieldDelegate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!