Objective c 失去对uisearchbar的关注时退出键盘
我正在应用程序的导航栏中创建UISearchBar选项。 我的应用程序由多个视图和子视图组成。 我有一个主要观点,对他自己有三个其他观点。其中一个是空的(目前),另外两个上面有TableView 我希望我的键盘在搜索时显示,在实际搜索或在uisearchbar外触摸/单击时隐藏。 我正在按要求使用searchbardelegate 我可以通过以下方式使用[searchBar resignFirstResponder]隐藏键盘Objective c 失去对uisearchbar的关注时退出键盘,objective-c,cocoa-touch,uisearchbar,uisearchbardelegate,Objective C,Cocoa Touch,Uisearchbar,Uisearchbardelegate,我正在应用程序的导航栏中创建UISearchBar选项。 我的应用程序由多个视图和子视图组成。 我有一个主要观点,对他自己有三个其他观点。其中一个是空的(目前),另外两个上面有TableView 我希望我的键盘在搜索时显示,在实际搜索或在uisearchbar外触摸/单击时隐藏。 我正在按要求使用searchbardelegate 我可以通过以下方式使用[searchBar resignFirstResponder]隐藏键盘 当我按回车键时 当我手动取消搜索时 当我按任何键盘按钮进行搜索或取消
- 当我按回车键时
- 当我手动取消搜索时
- 当我按任何键盘按钮进行搜索或取消时
- 当我使用触摸屏的空白部分时
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; if ([mySearchBar isFirstResponder] && [touch view] != mySearchBar) { [mySearchBar resignFirstResponder]; } [super touchesBegan:touches withEvent:event]; }
- (BOOL) searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
[self.myViewController1.customView1 setUserInteractionEnabled:NO];
[self.myViewController2.customView2 setUserInteractionEnabled:NO];
[searchBar setShowsCancelButton:YES animated:[mySettings animation]];
return YES;
}
在我开始使用搜索栏的那一刻,我关闭了2个子视图上的用户交互
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if ([self.mySearchBar isFirstResponder] && [touch view] != self.mySearchBar)
{
[self.mySearchBar resignFirstResponder];
[self.myViewController1.customView1 setUserInteractionEnabled:YES];
[self.myViewController2.customView2 setUserInteractionEnabled:YES];
}
[super touchesBegan:touches withEvent:event];
}
然后,当我在搜索栏外单击/点击/触摸时,我首先退出键盘,它仍然是第一个响应者,然后我为这两个子视图重新设置userInteraction。
这样做的顺序是至关重要的
这段代码允许你在一个mainViewController中退出键盘,即使它挤满了大量的子视图。你试过这个吗
UISearchBar *searchBar;
接下来设置UISearchBar
的Getter和Setter属性
并调用any方法
[searchBar resignFirstResponder];