tags:

views:

302

answers:

5

I have a text field that is being shown in a UITableViewCell and I want to be able to hide the keyboard when the user touches anywhere else on the screen aside from the text field. I know about [field resignFirstResponder];, but I don't know how to intercept touches on the background of the UITableView in order to call "resignFirstResponder".

Any help would be greatly appreciated.

A: 

Try implementing the - (void)textFieldDidEndEditing:(UITextField *)textField method of the UITextFieldDelegate.

Stephen Darlington
A: 

It's very simple: create a transparent UIView object that receives touches in the area you want, and when the touch is in the bounds of that view, call resignFirstResponder.

// somewhere in a view controller
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:backgroundView];

// in the touchesBegan:withEvent: method, for example
UITouch *touch = [[event allTouches] anyObject];

if ([field isFirstResponder] && [touch view] == backgroundView) {
    [field resignFirstResponder];
}

Alternatively, you could skip the backgroundView stuff and just add a conditional statement like the following in your touchesBegan:withEvent: method:

UITouch *touch = [[event allTouches] anyObject];

if ([field isFirstResponder] && [touch view] != field) {
    [field resignFirstResponder];
}

If the touch is ! (not) in the bounds of field, then you want to remove the keyboard.

Arseniy Banayev
For some reason, my touchesBegan method isn't being called. Any advice? My View and UITableView have userInteractionEnabled and multipleTouchEnabled to YES.
Raphael Caixeta
Be sure that the touchesBegan:withEvent: method has the correct signature and is implemented in a UIViewController, not in the view itself.
Arseniy Banayev
+2  A: 

The only way to do this is to subclass the UITableView, implement the touchesBegan method in your subclassed UITableView and send your UITextField objects to the UITableView. Here's how it should look like -

//  GRTableView.h
//  StackOverflow Example
//
//  Created by Raphael Caixeta on 8/13/10.
//  Copyright 2010 Raphael Caixeta. All rights reserved.
//

#import <UIKit/UIKit.h>


@interface GRTableView : UITableView <UITableViewDelegate> {

    UITextField *textField;

}

@property(nonatomic, retain) UITextField *textField;

@end

//
//  GRTableView.m
//  StackOverflow Example
//
//  Created by Raphael Caixeta on 8/13/10.
//  Copyright 2010 Raphael Caixeta. All rights reserved.
//

#import "GRTableView.h"

@implementation GRTableView
@synthesize textField;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [textField resignFirstResponder];
    [super touchesBegan:touches withEvent:event];       
}

- (void)dealloc {
    [super dealloc];
}


@end

And then in your regular file where you'll allocate a UITableView, just allocate the subclassed view and pass your textfield to the subclass. Hope that helps.

Raphael Caixeta
This is not "the only way to do this". See other valid answers in the list as well.
Chris Garrett
Try them out yourself. You'll see that clicking on the "background" of the UITableView will be blocked. Do you honestly think I'd have bothered creating an example if the other examples had worked?
Raphael Caixeta
A: 

hi iamdadude,

You can hide the key board in following conditions,

    • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { }
    • (void)TextField:(UITextField *)TxtFld textDidChange:(NSString *)searchText{ }

In this two condition you should place [TxtFld resignFirstResponder]; And also when your work for textfield is over you are performing some actions on that event also you should write [TxtFld resignFirstResponder]; where TxtFld is an object for UIText field.

By using this thing the keyboard will dismissed. The touch method you are writing may not work on the UITableviewCell. so this will be the way to hide the key board.

still it not work then specify the exact problem occurs.

iPhone Fun
A: 

There's an easier way to do this than the standard "subclass UITableView" or "add a subclass of UIView and handle touches". I create a UIButton with a clear background color over the area that I want to handle touches. When I add it to the view, I set its hidden property to YES.

When the text field begins editing, I set the button's hidden property to NO.

Then the button simply responds to UITouchUpInside control event, in a method which calls [textField resignFirstResponder] and button.hidden = YES.

Chris Garrett