Question

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

No correct solution

OTHER TIPS

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]];
}

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];
}

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];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top