问题描述
我有一个包含 UITextField 和 UITextView 的滚动视图.UITextField 返回键是Next",当按下它时,我调用 [myTextView becomeFirstResponder];一个随机的新行被插入到我的 textview 中,所以我从第二行开始.我该如何避免这种情况?
I have a scroll view which contains a UITextField and a UITextView. The UITextField return key is "Next" and when this is pressed I call [myTextView becomeFirstResponder]; A random new line is inserted into my textview so I start on the second line. How do I avoid this?
同样重要的是要注意,当我直接点击 UITextView 而不是点击下一个键时,这不会发生.
Also important to note that this does not happen when I tap directly on the UITextView rather than tapping the next key.
提前致谢!
推荐答案
解决这个问题的一个方案是当你实现textFieldShouldReturn:
委托方法时,一定要返回NO
,而不是 YES
.通过返回 NO
,换行符不会传递到文本字段.
One solution to solve this is when you implement the textFieldShouldReturn:
delegate method, be sure to return NO
, and not YES
. By returning NO
, the newline isn't passed on to the text field.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// move to next field or whatever you need
[myTextView becomeFirstResponder];
return NO;
}
这篇关于UITextView 无意中添加了新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!