我的项目设计:
UINavigationController ---> UITableViewController ---> UIViewController
UISeachBar
navigationItem.titleView
我尝试使用UITapGestureRecognizer
,但它的工作原理却不如预期。键盘仅在我触摸UITableViewCell
时解除。
我希望UITableView
回复touchesBegan
,这样每当我触摸UITableViewCell
或只是滚动桌面时,我就可以让键盘消失。
除此之外,我的UIViewController
包含googlemap的视图,它确实响应touchesBegan
,但只有一次第一次触摸,之后,每次其他触摸都会被忽略。
任何人都可以帮忙吗?
太久了,没看过:我想解除UITableView
喜欢safari或chrome的虚拟键盘:触摸开始时,而不是结束时
答案 0 :(得分:2)
试试这个
[self.view endEditing:YES];
答案 1 :(得分:1)
试试这个......
添加覆盖整个视图(设备大小)的空imageview,然后为该imageview添加手势识别器,然后在回调方法中编写以下代码。
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIImageView *image;//your IBOutlet imageview
UITapGestureRecognizer *recog=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(called:)];
[image addGestureRecognizer:recog];
}
-(void)called:(UITapGestureRecognizer *)sender
{
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}
答案 2 :(得分:1)
尝试创建UITableView的子类,如下所示:
subclassTB.h
#import <UIKit/UIKit.h>
@interface subClassTB : UITableView
@end
subclassTB.m
#import "subclassTB.h"
@implementation subClassTB
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// do your stuff here
//NSLog(@"tap registered");
[super touchesBegan:touches withEvent:event];
}
@end
并在当前控制器中使用
初始化表 subclassTB *yourtable = [[subclassTB alloc]initWithFrame:youFrame style:yourStyle];