Question

I have tried this:

In .h file :

    @interface TouchLabelViewController : UIViewController<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *aTextField;        
-(IBAction)hideKeyboard:(id)sender;

In .m File :

-(IBAction)hideKeyboard:(id)sender{
    [(UITextField*)sender resignFirstResponder];
}

And also tried this.

In .h file :

- (void)viewDidLoad
{
    [super viewDidLoad];
    aTextField.delegate = self;
}
@property (strong, nonatomic) IBOutlet UITextField *aTextField;
-(BOOL) textFieldShouldReturn:(UITextField *)textField;

In .m file:

-(BOOL) textFieldShouldReturn:(UITextField *)textField{

    [aTextField resignFirstResponder];
    return YES;
}

But still whenever I touch return key it does not hide keyboard.

Was it helpful?

Solution

You can take Advantage of these(delegate) methods when you set the delegate to the Textfield.

YOu should Check your delegate whether you have set the delegate to the Textfield

set this In your .h Class

     YourViewController :   UIViewController <UITextFieldDelegate>

Now Where you created the TextField set delegate like below

       myTextField.delegate = self;

And further do same as you were doing

 -(BOOL) textFieldShouldReturn:(UITextField *)textField{
     [textField resignFirstResponder];
     return YES;
  }

EDIT: In case if you were presenting the Current as UIModalPresentationFormSheet Presentation modes may keep the keyboard visible when not required. Default implementation affects UIModalPresentationFormSheet visibility.that's why need to overriding it for hiding keyboard

  - (BOOL)disablesAutomaticKeyboardDismissal 
 {
   return NO; 
 }

OTHER TIPS

first set delegate for TextField

aTextField.delegate = self;

-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
     [textField resignFirstResponder];
    //[aTextField resignFirstResponder];
    return YES;
}

Make your UITextField an outlet and attach it in your XIB file and then set the delegate and in that write the code:

[texfieldName resignFirstResponder];

This will work.

Just implement the <UITextFieldDelegate> in the .m file as shown below

@interface xyzViewController () <UITextFieldDelegate>

xyzViewController will be the name of your view controller. Use this code:

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    [theTextField resignFirstResponder];
    return YES;
}

You use the Delegate in UITextField in XIB or Programatically.

 -(IBAction)hideKeyboard:(id)sender
{
    [your_textfield resignFirstResponder]; // your_textfield is an UITextField
}

give the delegate to your UITextField

like

- (void)viewDidLoad
{
    yourTextField.delegate = self;//
}

and in Delegate method..

-(BOOL) textFieldShouldReturn:(UITextField *)textField{
     [textField resignFirstResponder];
     return YES;
}

For the UITextField You can do it like this

In .h file

@interface ViewController : UIViewController<UITextFieldDelegate>

In .m file

- (void)viewDidLoad
{
   textField.delegate = self;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

And For the UITextView You can do it like this

In .h file

@interface ViewController : UIViewController<UITextViewDelegate>

In .m file

- (void)viewDidLoad
{
   textView.delegate = self;
}

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

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

    return YES;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top