jerrymouse
jerrymouse

Reputation: 17792

UITextView hide keyboard in iphone

I want to hide keyboard when a user presses return in UITextView object in iphone. However, mysteriously this is not working for UITextView but working for UITextField. I am unable to figure out why...

This is what I did:

1) I created a view based application in XCode4.

2) in .xib created UITextView, UITextField and UIButton objects

3) Marked both UITextField and UITextView delegates to File's Owner in Outlets

4) Added <UITextFieldDelegate> to @interface UIViewController in .h

5) Added textFieldShouldReturn function in .m

Here are the codes:

.h file

@interface keyboardDisappearViewController : UIViewController <UITextFieldDelegate>
{

    UITextView *textBoxLarge;
    UITextField *textBoxLittle;
}
@property (nonatomic, retain) IBOutlet UITextView *textBoxLarge;
@property (nonatomic, retain) IBOutlet UITextField *textBoxLittle;

- (IBAction)doSomething:(id)sender;
@end

.m file

- (BOOL) textFieldShouldReturn:(UITextField *)theTextField 
{
    NSLog(@"textFieldShouldReturn Fired :)");
    [textBoxLarge resignFirstResponder];
    [textBoxLittle resignFirstResponder];
    return YES;
}

Amazingly, the keyboard is disappearing in case of textBoxLittle (UITextField) but not in case of textBoxLarge(UITextView)

As a further check I, made the button to call function doSomething

- (IBAction)doSomething:(id)sender {
    [textBoxLarge resignFirstResponder];
    [textBoxLittle resignFirstResponder];
}

When I am pressing the button, keyboard is disappearing in both textboxes.

Its driving me nuts why textFieldShouldReturn is working for small textbox, but NOT for large textbox.

Please Help!

Upvotes: 9

Views: 36433

Answers (7)

Sven Pfeiffer
Sven Pfeiffer

Reputation: 66

If you came here looking for the Swift solution, like I did, here you are :)

extension keyboardDisappearViewController : UITextViewDelegate {
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    if(text == "\n") {
        textView.resignFirstResponder()
    }
    return true
}}

Upvotes: 0

J-Dizzle
J-Dizzle

Reputation: 5143

Key Points

  • Someone needs to be listening for the Return Key press

    • UITextFieldDelegate
    • textFieldShouldReturn(textField : UITextField)
  • Someone needs to manually dismiss the keyboard

    • resignFirstResponder()

#1: Create a UITextFieldDelegate and assign it as the delegate to your UITextField -

exampleTextField.delegate = yourUITextFieldDelegate;

#2: Have 'yourUITextFieldDelegate' contain the following -

func textFieldShouldReturn(textField : UITextField) -> Bool {

    self.titleField.resignFirstResponder();   //Here's the key!!!!!

    return true;                              //true just says 'default behavior'
} 

Upvotes: 0

Nvork
Nvork

Reputation: 131

Use this code

Inherit UITextViewDelegate protocol in your Viewcontroller add the text

@interface YourViewController () <UITextViewDelegate>

In viewDidLoad set yourself as a delegate:

yourUITextView.delegate = self;

Implement the delegate method below:

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{ 
   return NO; 
}

Upvotes: 0

Ranga
Ranga

Reputation: 821

Simple trick Set delegate for your text view and then

doSomething 
{

}

action connect to ext view for control event didEndOnExit and tuchupinside


// To dismiss key board when user clicks enter/return

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
{
    if([text isEqualToString:@"\n"]) 
    {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

Upvotes: 5

Aaron Leanage
Aaron Leanage

Reputation: 1

You have coded in the .h file:

@interface keyboardDisappearViewController : UIViewController <UITextFieldDelegate>
{
    UITextView *textBoxLarge;
    UITextField *textBoxLittle;
}
@property (nonatomic, retain) IBOutlet UITextView *textBoxLarge;
@property (nonatomic, retain) IBOutlet UITextField *textBoxLittle;

- (IBAction)doSomething:(id)sender;
@end

It should be:

@interface keyboardDisappearViewController : UIViewController <UITextFieldDelegate>
{
    UITextView *textBoxLarge;
    UITextField *textBoxLittle;
}
@property (nonatomic, retain) IBOutlet UITextField *textBoxLarge;
@property (nonatomic, retain) IBOutlet UITextField *textBoxLittle;

- (IBAction)doSomething:(id)sender;
@end

Upvotes: -1

Roger C S Wernersson
Roger C S Wernersson

Reputation: 6562

Three things:

Make your view implement UITextViewDelegate.

@interface keyboardDisappearViewController : UIViewController
    <UITextFieldDelegate, UITextViewDelegate>

Add the following method:

- (BOOL)textView:(UITextView *)textView
        shouldChangeTextInRange:(NSRange)range
        replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
    }
    return YES;
}

Set the file's owner as delegate for the UITextView in the interface builder.

(BTW: Solution copied from the comments to the previous answer, as it took me a while to extract. I though others could benefit from my experience.)

Upvotes: 20

Abdurrashid Khatri
Abdurrashid Khatri

Reputation: 338

You need to write code in UITextViewDelegate and assign it to your class.

Upvotes: 7

Related Questions