0
我已經觸摸了方法,但只有當我點擊視圖的某些區域時纔有效,並不是無處不在。當觸摸文本區域外部時,並不總是會關閉鍵盤
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];// this will do the trick
}
我該如何解決這個問題?
感謝
我已經觸摸了方法,但只有當我點擊視圖的某些區域時纔有效,並不是無處不在。當觸摸文本區域外部時,並不總是會關閉鍵盤
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];// this will do the trick
}
我該如何解決這個問題?
感謝
OK,嘗試用下面的代碼&檢查:
-(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];
}
你可以檢查,如果沒有其他意見overlaping和接收您的觸摸事件? – vodkhang 2013-03-04 23:52:32
我有一個滾動視圖,可能會在視圖的其他區域收到touchesBegan。所以我如何解決這個問題? – 2013-03-04 23:57:23