编辑完textField后,我不会问如何隐藏键盘。我的问题是:有没有办法在每个视图上执行此操作? (如设置)还是我需要编写以下两个函数并每次都正确设置委托?在每个视图上隐藏键盘 - Swift
func textFieldShouldReturn(textField: UITextField) -> Bool // called when 'return' key pressed. return NO to ignore.
{
textField.resignFirstResponder()
return true;
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
我发展了很多文本框(以及视图)的应用程序,所以我会尽量避免冗余代码。避免这种重复的最佳解决方案是什么?
谢谢!
写自己的自定义文本字段(如'JibeTextField'),这是也是UITextField的子类,然后覆盖你想它的任何方法。学习如何使用这个项目编写https://github.com/dhawaldawar/TextFieldValidator。他在Objective-C中写道。如果你不能,明天我会为你写信:D – t4nhpt