我想實現一個功能,一旦我觸摸表視圖,視圖頂部的搜索欄將resignFirstResponder和鍵盤將從視圖中檢索。我將下面的代碼與xib文件中的tableView關聯起來,但它似乎不起作用。ResignFirstResponder無法正常工作。
- (IBAction)backgroundTap:(id)sender
{
NSLog(@"test : did touch down");
[_searchBar resignFirstResponder];
}
我嘗試通過在.m文件中添加以下代碼的其他方式,但仍然無法工作。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"test : did touch down");
UITouch *touch = [touches anyObject];
UIView *view = (UIView *)[touch view];
if (view == self.view) {
[_searchBar resignFirstResponder];
}
}
希望有人能幫忙。
我在類似的情況下使用你的第二種方法,它工作正常,除了在iOS 7上,resignFirstResponder在第一次點擊時被忽略。同樣使用[self endEditing:YES]。第二次點擊正常工作。解決方法是每次輕敲兩次,然後調用resignFirstResponder,然後運行。 – user1055568