0

enter image description here

in the following scenario clicking on the pencil turns the iz 1 text to be editable by making it becomeFirstResponder and the keyboard opens, I wish to close the keyboard when clicking on the "empty rows" or iz2.

how can i do that?

I've tried adding to the cellView

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self dismissKeyboard];
}

but it didn't work

liv a
  • 3,232
  • 6
  • 35
  • 76

3 Answers3

1

You can hide keyboard using this:

[self.view endEditing:YES];

EDIT

Add gesture in where you can call becomeFirstResponder.

- (void)showKeyboard
{
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
    [self.tableView addGestureRecognizer:gesture];
}

and remove it in hide method,

- (void)hide
{
    [self.view endEditing:YES];
    [self.view removeGestureRecognizer:self.view.gestureRecognizers[0]];
}
caglar
  • 1,079
  • 1
  • 9
  • 17
0

Just call:

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

Or:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSIndexPath *indexPathForYourCell = [NSIndexPath indexPathForRow:0 inSection:0];
    YourCustomCell *cell = [self tableView:self.tableView cellForRowAtIndexPath:indexPathForYourCell];

    [cell.iz1TextField resignFirstResponder];
}
Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
  • the above does work, probably your -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event wasn't called at all? – Volker Mar 03 '14 at 18:29
  • @liva can you set a breakpoint in `touchesBegan:` to see if it is called? – Rafał Sroka Mar 03 '14 at 18:30
  • where do you place the code in the `viewcell` or `tableviewcontroller`? because the cell doesn't have self.view so i wrote `[self endEditing:YES]` and the functions is being called – liv a Mar 03 '14 at 18:31
  • Try calling: `[cell.iz1TextField resignFirstResponder]`. You can access this text view by getting a cell using `UITableView`'s `cellForRowAtIndexPath`. – Rafał Sroka Mar 03 '14 at 18:34
  • when i place the code in the uitableviewcontroller it doesn't get called – liv a Mar 03 '14 at 18:37
  • Try placing it in the cell. – Rafał Sroka Mar 03 '14 at 18:40
0

my final solution was something similar to what @caglar suggested here is the code i've written inside tableviewcontroller:

- (void)viewDidLoad
{
    [super viewDidLoad];

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

-(void)dismissKeyboard
{
    [self.view endEditing:YES];
}
liv a
  • 3,232
  • 6
  • 35
  • 76