当移动到另一个视图控制器时,UISearchBar 不会隐藏 resignFirstResponder 上的键盘
我有一个行为正常的 UISearchBar - 如果我点击“搜索”或“取消”,键盘就会消失。
但是,当我在导航堆栈上推送新的视图控制器时,如果键盘打开,它将不会关闭。它保留在旧视图控制器中,如果我导航回它,键盘仍然会显示。
我很困惑,因为我的 searchBarShouldEndEditing 方法按预期被调用,并且我执行了 [activeSearchBar resignFirstResponder]。这适用于“搜索”或“取消”,但不适用于由视图消失触发的情况。
我的代表代码:
#pragma mark Search bar delegate methods__________________________
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)activeSearchBar
{
NSLog(@"searchBarShouldBeginEditing");
[activeSearchBar setShowsCancelButton:TRUE animated:YES];
if (!showSearchType)
return TRUE;
if([UIView respondsToSelector:@selector(animateWithDuration:animations:)]) {
// iOS 4.0 and later
[UIView animateWithDuration:0.3 animations:^ {
searchTypeView.frame = CGRectMake(0, 44, 320, 69);
searchTypeView.bounds = CGRectMake(0, 0, 320, 69);
grayBg.alpha = 0.6;
}];
} else {
// Before iOS 4.0
searchTypeView.frame = CGRectMake(0, 44, 320, 69);
searchTypeView.bounds = CGRectMake(0, 0, 320, 69);
grayBg.alpha = 0.6;
}
self.frame = CGRectMake(0, 0, 320, 113);
[self bringSubviewToFront:searchTypeView];
[self bringSubviewToFront:activeSearchBar];
return TRUE;
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)activeSearchBar
{
NSLog(@"searchBarShouldEndEditing");
if ([activeSearchBar isFirstResponder]) {
NSLog(@"activeSearchBar isFirstResponder");
}
[activeSearchBar resignFirstResponder];
[activeSearchBar setShowsCancelButton:FALSE animated:YES];
if (!showSearchType)
return TRUE;
if([UIView respondsToSelector:@selector(animateWithDuration:animations:)]) {
// iOS 4.0 and later
[UIView animateWithDuration:0.3 animations:^ {
searchTypeView.frame = CGRectMake(0, 9, 320, 69);
searchTypeView.bounds = CGRectMake(0, 0, 320, 0);
grayBg.alpha = 0;
}];
} else {
// Before iOS 4.0
searchTypeView.frame = CGRectMake(0, 9, 320, 69);
searchTypeView.bounds = CGRectMake(0, 0, 320, 0);
grayBg.alpha = 0;
}
self.frame = CGRectMake(0, 0, 320, 44);
return TRUE;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)activeSearchBar {
NSLog(@"searchBarTextDidEndEditing");
if ([activeSearchBar isFirstResponder]) {
NSLog(@"activeSearchBar isFirstResponder");
}
[activeSearchBar resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)activeSearchBar
{
NSLog(@"searchBarCancelButtonClicked");
[activeSearchBar resignFirstResponder];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)activeSearchBar
{
NSLog(@"searchBarSearchButtonClicked");
self.query = searchBar.text;
[activeSearchBar resignFirstResponder];
[searchView startSearch];
}
此输出:
2011-12-07 20:00:33.061 MyApp[55725:307] searchBarShouldEndEditing
2011-12-07 20:00:33.063 MyApp[55725:307] activeSearchBar isFirstResponder
2011-12-07 20:00:33.066 MyApp[55725:307] searchBarTextDidEndEditing
感谢您的任何想法!
I have a UISearchBar that behaves normally - the keyboard goes away if I hit "Search" or "Cancel".
However, when I push a new view controller on my navigation stack, if the keyboard is open, it won't close. It stays there in the old view controller, and if I navigate back to it the keyboard is still shown.
I'm stumped because my searchBarShouldEndEditing method is called as expected, and I do [activeSearchBar resignFirstResponder]. This works for "Search" or "Cancel", but not when it's triggered by the view disappearing.
My delegate code:
#pragma mark Search bar delegate methods__________________________
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)activeSearchBar
{
NSLog(@"searchBarShouldBeginEditing");
[activeSearchBar setShowsCancelButton:TRUE animated:YES];
if (!showSearchType)
return TRUE;
if([UIView respondsToSelector:@selector(animateWithDuration:animations:)]) {
// iOS 4.0 and later
[UIView animateWithDuration:0.3 animations:^ {
searchTypeView.frame = CGRectMake(0, 44, 320, 69);
searchTypeView.bounds = CGRectMake(0, 0, 320, 69);
grayBg.alpha = 0.6;
}];
} else {
// Before iOS 4.0
searchTypeView.frame = CGRectMake(0, 44, 320, 69);
searchTypeView.bounds = CGRectMake(0, 0, 320, 69);
grayBg.alpha = 0.6;
}
self.frame = CGRectMake(0, 0, 320, 113);
[self bringSubviewToFront:searchTypeView];
[self bringSubviewToFront:activeSearchBar];
return TRUE;
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)activeSearchBar
{
NSLog(@"searchBarShouldEndEditing");
if ([activeSearchBar isFirstResponder]) {
NSLog(@"activeSearchBar isFirstResponder");
}
[activeSearchBar resignFirstResponder];
[activeSearchBar setShowsCancelButton:FALSE animated:YES];
if (!showSearchType)
return TRUE;
if([UIView respondsToSelector:@selector(animateWithDuration:animations:)]) {
// iOS 4.0 and later
[UIView animateWithDuration:0.3 animations:^ {
searchTypeView.frame = CGRectMake(0, 9, 320, 69);
searchTypeView.bounds = CGRectMake(0, 0, 320, 0);
grayBg.alpha = 0;
}];
} else {
// Before iOS 4.0
searchTypeView.frame = CGRectMake(0, 9, 320, 69);
searchTypeView.bounds = CGRectMake(0, 0, 320, 0);
grayBg.alpha = 0;
}
self.frame = CGRectMake(0, 0, 320, 44);
return TRUE;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)activeSearchBar {
NSLog(@"searchBarTextDidEndEditing");
if ([activeSearchBar isFirstResponder]) {
NSLog(@"activeSearchBar isFirstResponder");
}
[activeSearchBar resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)activeSearchBar
{
NSLog(@"searchBarCancelButtonClicked");
[activeSearchBar resignFirstResponder];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)activeSearchBar
{
NSLog(@"searchBarSearchButtonClicked");
self.query = searchBar.text;
[activeSearchBar resignFirstResponder];
[searchView startSearch];
}
This outputs:
2011-12-07 20:00:33.061 MyApp[55725:307] searchBarShouldEndEditing
2011-12-07 20:00:33.063 MyApp[55725:307] activeSearchBar isFirstResponder
2011-12-07 20:00:33.066 MyApp[55725:307] searchBarTextDidEndEditing
Thanks for any ideas!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您也可以:在 .h 文件中声明 searchBar 变量,创建属性等...然后在按钮单击事件方法上,推送另一个视图,编写第一行:
Also you can : declare searchBar variable in your .h file, make property etc... then on your button click event method, that pushes another view, write first line :
好吧,我找到了一个解决方案,虽然不是很好。
在父视图控制器中,我将以下行放在任何会触发新视图控制器推送到导航堆栈上的内容之前:
这似乎有点脆弱且容易出错...我很想知道是否有更好的方法。
OK, I found a solution, although it's not great.
In the parent view controller, I put the following line BEFORE anything that would trigger a new view controller to be pushed on the navigation stack:
This seems a bit fragile and error-prone... I would love to know if there's a better way.