0

I am trying to figure out how to dismiss the keyboard and trigger a method when the user taps outside of a UITextField

in TableViewCell.m:

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self.delegate cellDidBeginEditing:self];
}

in ViewController.m:

-(void)cellDidBeginEditing:(TableViewCell *)editingCell
{
_editingOffset = _tableView.scrollView.contentOffset.y - editingCell.frame.origin.y;
for (TableViewCell *cell in [_tableView visibleCells]) {
    [UIView animateWithDuration:0.3
                     animations:^{
                         cell.frame = CGRectOffset(cell.frame, 0, _editingOffset);
                         if (cell != editingCell) {
                             cell.alpha = 0.25;

                         }
                    }];  
    } 
}

the cellDidBeginEditing: method displaces the cell to the top and shades the other cells grayish.

I have another method, cellDidEndEditing: which does the opposite of this, and the code is not really needed.

As of now, selecting, for example, "cell2" when editing "cell1" just triggers cellDidBeginEditing for "cell2"

I want the keyboard to dismiss and cellDidEndEditing to trigger when I click outside of "cell1"

Brian
  • 73
  • 2
  • 11
  • possible duplicate of [iphone, dismiss keyboard when touching outside of textfield](http://stackoverflow.com/questions/5306240/iphone-dismiss-keyboard-when-touching-outside-of-textfield) – Ben Flynn May 01 '14 at 22:08

1 Answers1

1

Try implementing and calling these functions:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesBegan:withEvent");
    [self.view endEditing:YES];
    [super touchesBegan:touches withEvent:event];
}

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

This should make it so that when you touch any where outside of the two textFields, the keyboard automatically dismisses.

CaptJak
  • 3,592
  • 1
  • 29
  • 50
  • i put the first method in ViewController.m and it didnt do anything. and the i dont have "firstTextField" and "secondTextField" as objects under TableViewCell. i was thinking about UITapGesture but could use guidance. – Brian Jul 17 '13 at 03:25
  • @Brian post some more code showing where you have the text fields in your .m folder. The `resignFirstResponder` is what should dismiss the keyboard. If you want to use a touch gesture, you would use `[self.view endEditing:YES];` when you touch. – CaptJak Jul 17 '13 at 13:36
  • `-(BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return NO; }` – Brian Jul 17 '13 at 17:47
  • @Brian Why are your returning `NO`? Try returning `YES` and if that don't work, try `self.textField resignFirstResponder]; return YES;` – CaptJak Jul 17 '13 at 17:58