0

Possible Duplicate:
Dismiss keyboard on touch anywhere outside UITextField

MyViewController in interface builder :

enter image description here

In my custom TableViewCell I have a TextField. My plan is disappear KeyPad when User Clicks outside the touchpad.

I could not make it works using UITapGestureRecognizer

in viewDidLoad:

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.tableView addGestureRecognizer:gestureRecognizer];

Then how should I make it work with cell.TextField ?

I read about this solution here:

[self.view endEditing:YES];

or

[[self.tableView superView] endEditing];

I couldn't make it work neither. Where should I use them?

Or if you have any better way, please let me know.

Edit

I could not make it work:

@property (nonatomic, strong) UITextField *cellTextField;

in viewDidLoad:

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [self.settingTableView addGestureRecognizer:gestureRecognizer];

And we have:

- (void) hideKeyboard {
    [self.cellTextField resignFirstResponder];
}

And :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


    self.cellTextField = cell.dataEdit;

What is my fault?

Community
  • 1
  • 1
Ali
  • 9,800
  • 19
  • 72
  • 152

5 Answers5

0

you could add a property to your view controller, say

@property (nonatomic, strong) UITextField *cellTextField

and then assign cell.TextField to it.

in hideKeyboard: you can dismiss the keyboard by doing

[cellTextField resignFirstResponder];

You should call resignFirstResponder on the textField itself, not the controller's view.

NikosM
  • 1,131
  • 7
  • 9
  • Please check my edit at the end of my question. I couldn't make it work. – Ali Nov 12 '12 at 15:54
  • Can you make sure that `hideKeyboard` is called when touching anywhere in the table? Put an `NSLog` statement to test if it's being called. – NikosM Nov 12 '12 at 16:49
  • Also, are you sure that cell.dataEdit contains a value? You need to link in Interface Builder, the textField you've visually added, to the cell's dataEdit property – NikosM Nov 12 '12 at 17:07
0

Add [cellTextField resignFirstResponder]; hope this works...

Maybe look at UITextFieldDelegate, and in particular - (BOOL)textFieldShouldReturn:(UITextField *)textField

Bot
  • 11,868
  • 11
  • 75
  • 131
lakshmen
  • 28,346
  • 66
  • 178
  • 276
  • Can you please check my edit at the end f the question and let me know if I have any mistake. – Ali Nov 12 '12 at 15:55
0

You Can hide Keyboard if You Put the Whole Screen Custom UIButton.Then Attached this Method With this Custom UIButton.set the UITextField tag =1 in Interface builder before implementation of this method.

-(IBAction)HideKeyboard
{
    [[self.view viewWithTag:1]resignFirstResponder];
}

Note: if You Want to hide keyboard by Click in CustomCell then Put this UIButton there or You Can Call this Method using Gesture Recognization as you did in Your Code without Using Custom UIButton. And also make sure You Connect UITextField delegate.Hope this Work .

NSCool
  • 229
  • 1
  • 12
0

You may try this.

- (void) hideKeyboard {
    [self.view endEditing:YES];
}
Siba Prasad Hota
  • 4,779
  • 1
  • 20
  • 40
0

You can achieve this task by inserting the following method

You must implement UITextFieldDelegate

you must set a tag for the textfield in the storyboard or in when you start loading the cells of the table

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.currentIndexPath = [(UITableView*)textField.superview.superview.superview indexPathForCell:(UITableViewCell*)textField.superview.superview];
    self.currentTableView = (UITableView*)textField.superview.superview.superview;
    self.keyboardIsShown = YES;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];
    if(self.keyBoardIsShown)
    {
        [self dismissKeyboard:currentPoint UsedTableView:self.myTableView IndexPathForRow:self.currentIndexPath TexFieldTag:tag];
        self.keyBoardIsShown = NO;
    }
}

-(void)dismissKeyboard:(CGPoint)currentPoint UsedTableView:(UITableView*)table IndexPathForRow:(NSIndexPath*)indexPath TexFieldTag:(int)tag
{
    UITableViewCell *cell = [table cellForRowAtIndexPath:indexPath];
    if(!CGRectContainsPoint([self.view convertRect:[cell viewWithTag:tag].frame fromView:[cell viewWithTag:tag].superview.superview], currentPoint))
    {
        [(UITextField*)[cell viewWithTag:tag] resignFirstResponder];
    }
}
Ahmed Hammad
  • 435
  • 4
  • 8