2013-03-04 79 views
0

我已经触摸了方法,但只有当我点击视图的某些区域时才有效,并不是无处不在。当触摸文本区域外部时,并不总是会关闭键盘

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [self.view endEditing:YES];// this will do the trick 
} 

我该如何解决这个问题?

感谢

+0

你可以检查,如果没有其他意见overlaping和接收您的触摸事件? – vodkhang 2013-03-04 23:52:32

+0

我有一个滚动视图,可能会在视图的其他区域收到touchesBegan。所以我如何解决这个问题? – 2013-03-04 23:57:23

回答

0

OK,尝试用下面的代码&检查:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    [touch locationInView:self.view]; 
    [self.view endEditing:YES]; 
} 
0

试试这个

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)]; 
[self.view addGestureRecognizer:singleTap]; 

// Do any additional setup after loading the view, typically from a nib. 
} 
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture 
{ 
//Get touch point 
CGPoint touchPoint=[gesture locationInView:self.view]; 

//Hide keyBoard 
[self.view endEditing:YES]; 
}