在文本区域外触摸时并不总是解雇键盘

时间:2013-03-04 22:58:18

标签: iphone cocoa-touch

我有触摸方法,但只有当我点击视图的某些区域而不是无处不在时它才有效。

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

我该如何解决这个问题?

由于

2 个答案:

答案 0 :(得分:0)

好的,试试下面的代码&检查:

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

答案 1 :(得分: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];
}