UITextView adding new line unintentionally?
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;}
Okay, this behaviour is due to a bug in iOS when becoming firstresponder within same run loop by using next button. To over come this you should do this manually. First resign first responder from a textFied, and then make textView as a first responder. like this. Implement this delegate method.
- (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; [textView performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.0]; return YES;}
Similar idea to performSelector
, but using asynchronous dispatch queue (Swift 4 version):
func textFieldShouldReturn(_ textField: UITextField) -> Bool { textField.resignFirstResponder() DispatchQueue.main.async { self.myTextView.becomeFirstResponder() } return true}