1

I'd like to thanks you in advance for your answers. I read all stuff about this but I couldn't solve it.

I know how to hide keyboard on UITextField, just like this:

    - (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [placename resignFirstResponder];
    [address resignFirstResponder];

    return YES;
}

But I also have an UITextView and I don't know how to hide it. I don't want to hide it with a button I want to hide it with a "Done" in the return.

I read something like this:

[textField setReturnKeyType: UIReturnKeyDone];

But... how and where to implement it? I'm new on this so I need to know it step by step,please.

Thanks so much. (Sorry I'm not able to send an image...new user ;) )

Icarox
  • 63
  • 10

2 Answers2

4

Its easy. Have textview delegate in your controller and add following method.

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

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

    return YES;
}

There is one disadvantage using this, user can not write multi line statements. To solve this you can have a create view over keyboard and have button over there and add action to resign your textview.

Credit to this question

Community
  • 1
  • 1
Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
0

Additional to @JanakNirmal Answers, the code for the .h and .m. Works fine for me.

.h

@interface ViewController : UIViewController <UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *textField; // connected with IB
@end

.m

@interface ViewController ()
- (void)viewDidLoad
{
    [super viewDidLoad];

    // Done key and hide keyboard 
    [self.textField setDelegate:self];
    [self.textField setReturnKeyType:UIReturnKeyDone];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range  replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
    }
    return YES;
}
@end
jerik
  • 5,714
  • 8
  • 41
  • 80