1

Ok I have exhausted google and this site looking for an answer and cannot find one. I currently have a UITextView (postMessage) and the keyboard for it will not disappear. I have tried every possible code combination to make it dismiss but it simply will not.

I have tried doing the return key method

- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@"\n"]){
    //MainViewController *mvc = [[MainViewController alloc] init];
    //[mvc.view endEditing:YES];
    //[[self view] endEditing: YES];
    //[self.postMessage becomeFirstResponder];
    //[self.topLayer endEditing:YES];
    //[self.view endEditing:YES];
    //[self.collectionView endEditing:YES];
    //[self.postMessage.editable = NO];
    //[postMessage resignFirstResponder];
    [self.postMessage resignFirstResponder];
    return NO;
}else{
    return YES;
}
}

I also have tried using a UIToolBar with a UIBarButton

-(IBAction)done:(id)sender {
[self keyboardIsHidden];
[[self view] endEditing: YES];
[self.postMessage becomeFirstResponder];
[self.topLayer endEditing:YES];
[self.view endEditing:YES];
[self.collectionView endEditing:YES];
//[self.postMessage.editable = NO];
[self.postMessage resignFirstResponder];

keyboardBar.hidden = YES;
NSLog(@"done");
}

And nothing..... I have used NSLogs to make sure that both methods are being called, but the keyboard will not resign. I have also use and if statement to check to make sure the UITextView (postMessage) is firstResponder and it is.

In my project I have 2 views (not view controllers) a bottom layer view and top layer. The user pulls the top layer up into view. I also have a UICollectionView.

I have noticed that when I press the return key on the keyboard or the 'Done' key on my UIToolBar the UICollectionView reloads the data.

I do have the UITextViewDelegate set on the .h. I also have postMessage.delegate = self in my ViewDidLoad

Thanks! -Mike

MBarton
  • 2,087
  • 2
  • 14
  • 17

3 Answers3

1
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    if([string isEqualToString:@"\n"]) {
        [textField resignFirstResponder];
        return NO;
    }

    return YES;
}

i am using this work fine

Jignesh B
  • 490
  • 7
  • 18
0

you can use like this.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
 replacementText:(NSString *)text
{

if ([text isEqualToString:@"\n"]) {

    [textView resignFirstResponder];
    // Return FALSE so that the final '\n' character doesn't get added
    return NO;
}
// For any other character return TRUE so that the text gets added to the view
return YES;
}

or create a button ..

-(IBAction)btnclicked{

[txtvw resignFirstResponder];
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
gajchtr
  • 48
  • 5
0

Attach your TextField Delegate with File owner , Clean build and run Project

Dipak Narigara
  • 1,796
  • 16
  • 18