我有一个自定义键盘,它设置了一个 UITextInput 委托,用于将文本发送到当前文本字段。但我还需要发送一个“返回”按钮来调用 textFieldShouldReturn 方法,据我所知,UITextInput 不允许这样做。(除非返回键有某种特定字符?)
那么究竟如何将“return”按键值传递给文本字段以触发 textFieldShouldReturn?
我有一个自定义键盘,它设置了一个 UITextInput 委托,用于将文本发送到当前文本字段。但我还需要发送一个“返回”按钮来调用 textFieldShouldReturn 方法,据我所知,UITextInput 不允许这样做。(除非返回键有某种特定字符?)
那么究竟如何将“return”按键值传递给文本字段以触发 textFieldShouldReturn?
返回键的特定字符是\n
将其添加到字符串的末尾会将光标放在下一行...
*然后实际调用 textFieldShouldReturn 方法,如果您出于某种原因仍想这样做,您只需像调用任何方法一样调用它。
你应该实现这个:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
You're headed in the wrong direction here, because you're already using a custom keyboard. That should eliminate you from needing to use a UITextFieldDelegate to solve the problem of return key detection.
textFieldShouldReturn: is a delegate method of the UITextField used to detect when the user presses the return key. If you didn't have this method, you'd have to use textField:shouldChangeCharactersInRange:replacementString: to detect the newline character from a normal UIKeyboard, which would be a pain in the butt.
But if you have a particular button on your keyboard that should do something special, just wire that button up to your IBAction method directly.
So something like this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self returnKeyPressed:textField];
return NO;
}
- (IBAction)returnKeyPressed:(id)sender {
// Do whatever you want done
}
If you wire your custom key to returnKeyPressed:, both a hardware keyboard and your virtual custom keyboard would end up in returnKeyPressed: and the behavior would be consistent.
You probably would want to define a small protocol to make sure your delegates support returnKeyPressed: in addition to the UITextFieldDelegate methods.