Iphone 触摸文本字段外部时不总是关闭键盘

Iphone 触摸文本字段外部时不总是关闭键盘,iphone,cocoa-touch,Iphone,Cocoa Touch,我有触摸方法,但它只有在我点击视图的某些区域时才起作用,而不是在任何地方 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.view endEditing:YES];// this will do the trick } 我如何解决这个问题 谢谢好的,请尝试以下代码并检查: -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)eve

我有触摸方法,但它只有在我点击视图的某些区域时才起作用,而不是在任何地方

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];// this will do the trick
}
我如何解决这个问题


谢谢

好的,请尝试以下代码并检查:

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

- (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];
}

您能检查一下是否没有其他视图重叠并接收您的触摸事件吗?我有一个scrollview,它可能接收触摸开始于视图的其他区域。那么我该如何解决这个问题呢?