开发者

Proper way to resignFirstResponder when a UITableView is touched?

开发者 https://www.devze.com 2023-01-01 06:35 出处:网络
I have a pretty simple UITableView who\'s cells contain UITextFie开发者_运维百科lds and I need to be able to call resignFirstResponder to hide the keyboard whenever a user touches the UITableView outs

I have a pretty simple UITableView who's cells contain UITextFie开发者_运维百科lds and I need to be able to call resignFirstResponder to hide the keyboard whenever a user touches the UITableView outside of one of the cells.

I have read this question/answer but it seems like a very rudimentary way to achieve this. I have read about a way to do it by converting the UITableView to a UIControl so that you can connect the TouchDown event.

Does anybody know the standard or preferred way to achieve this functionality?


A tap gesture recognizer will detect taps, however I have sometimes found that it interfered with the normal row selection mechanism. I think I've come up with a good solution in my answer to this question. Where I've posted some example code of a UIView subclass that you set as the accessoryInputView of the textfield. Then if you select the textfield it adds the tap recognizer automatically, and then automatically removes it when you've finished.


This is pretty intresting , but not to very hard, for that you have to use UITapGestureRecognizer...

in .h

IBOutlet UITextField *txtField;
IBOutlet UITableView *tableview;

in .m

-(void)viewDidLoad
{

UITapGestureRecognizer *tapgesture=[[UITapGestureRecognizer alloc]initWithTarget:self 
action:@selector(tableClicked)];
[tableview addGestureRecognizer:tapgesture];

[super viewDidLoad];

}

-(void)tableClicked
{

     [txtField resignFirstResponder];

}

fallow this example code , i think it works...Thank you..

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号