问题描述
我有一个包含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:
委托方法,请务必返回否
,而不是 YES
。通过返回否
,换行符不会传递到文本字段。
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无意中添加新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!