2

I'm trying to get the keyboard to disappear when the screen is touched, a question that is answered all over stackoverflow. I was able to get the keyboard to disappear when the enter key was pressed thanks to a thread here. I'm not having luck on the background touch resigning the first responder. The method is being entered, I have an NSLog in the method saying, "in backgroundTouched" but the keyboard is still there.

I've tried making the UIView a UIControl class so I could use the touch event. journalComment is a UITextView.

-(IBAction)backgroundTouched:(id)sender
    {
        [journalComment resignFirstResponder];
        NSLog(@ "in backgroundTouched");

}

I've also tried having a invisible button under everything that calles the backGroundTouched method. I think it maybe that I'm missing something in interface builder, but I'm not sure what.

Thank you for any help!

This is what works for the done button:

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range 
 replacementText:(NSString *)text
{
    // Any new character added is passed in as the "text" parameter
    if ([text isEqualToString:@"\n"]) {
        // Be sure to test for equality using the "isEqualToString" message
        [textView resignFirstResponder];

        // Return FALSE so that the final '\n' character doesn't get added
        return FALSE;
    }
    // For any other character return TRUE so that the text gets added to the view
    return TRUE;
}
Andres Canella
  • 3,706
  • 1
  • 35
  • 47
rd42
  • 3,584
  • 15
  • 56
  • 68

2 Answers2

3

I found the following code works best with my text view (not text field) without the delegate methods:

first you set up a tap gesture recognizer onto your view :

- (void)viewDidLoad{

UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc]
        initWithTarget:self
                action:@selector(tap:)];
    tapRecognizer.delegate = self;
    [self.view addGestureRecognizer:tapRecognizer];

}

and then in your tap method :

- (void)tap:(id)sender
{
// use to make the view or any subview that is the first responder resign (optionally force)    
[[self view] endEditing:YES];
}

this should allow your keyboard to be dismissed when you anywhere on the view.

Hope this helps

xRab
  • 133
  • 1
  • 4
  • Hi xRab , good answer, thank you for your suggestions [here](http://stackoverflow.com/a/39306423/1894067) If you are always interested to this kind of thread, I've improve my code to the latest swift – Alessandro Ornano Jan 20 '17 at 08:43
1

Try this. We had this problem eariler, but eventually found the right solution.

  -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

        [yourtextfield resignFirstResponder];

        // you can have multiple textfields here


    }

This should resolve the problem with the keyboard not dissapearing when pushing the background.

Jens Bergvall
  • 1,617
  • 2
  • 24
  • 54
  • Thanks. Sorry for the newb question, but once I put that in my .m and .h file how do I tie it to the text field in interface builder? Is that a ctrl drag from files owner to the text field selecting the UITextField I've created programmatically? – rd42 Apr 12 '11 at 12:57
  • @rd42 You should just need to add that snippet to your .m file and it should work. If not, feel free to add more of your code. – Jens Bergvall Apr 12 '11 at 13:35
  • Ok if I figure out what other code would be helpful :) , I'll put it in. Thanks for your help. – rd42 Apr 12 '11 at 13:56