I'm getting problem in resigning the keyboard after clicking done button. I'm using textView
-(BOOL)textViewShouldReturn:(UITextView *)textView
{
if(textView == addressView)
{
if(isNotif)
{
[self setViewMovedUp:NO];
}
textView.text= [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[addressView resignFirstResponder];
}
return YES;
}
Instead of keyboard resigning the cursor is coming to new line in the text field. Please help me.
Thank You Praveena.
Fixed KingOfBliss's answer:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (textView == messageInput) {
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
}
return YES;
}
Use the following delegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (textView == YourTextField){
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
}
return YES;
}
Usually in textView the return key is use to add \n to the text, so its better to add some other button to top of the UItextView and code the resigning function there.
EDIT:
There is no such delegate -(BOOL)textViewShouldReturn:(UITextView *)textView
this will happen if the ViewController that is above this textView in the view hierarchy is not the delegate of this textView. If it is not then the ViewController will never get the message textViewShouldReturn. In the viewController after the subView (the UITextView) is created.
aTextView.delegate = self;
To check to make sure it is getting called add this to your function and test
NSLog(@"resigning first responder");
this will test to see if this function is even getting called
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With