iphone - 移动到另一个 View Controller 时,UISearchBar 不会在 resignFirstResponder 上隐藏键盘

标签 iphone ios uisearchbar resignfirstresponder

我有一个正常运行的 UISearchBar - 如果我点击“搜索”或“取消”,键盘就会消失。

但是,当我在我的导航堆栈上推送一个新的 View Controller 时,如果键盘打开,它不会关闭。它保留在旧 View Controller 中,如果我导航回它,键盘仍会显示。

我很困惑,因为我的 searchBarShouldEndEditing 方法按预期被调用,而我确实 [activeSearchBar resignFirstResponder]。这适用于“搜索”或“取消”,但不适用于由 View 消失触发的情况。

我的委托(delegate)代码:

#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

感谢任何想法!

最佳答案

您还可以:在您的 .h 文件中声明 searchBar 变量,创建属性等...然后在您的按钮点击事件方法上,推送另一个 View ,写第一行:

[yourSearchBar resignFirstResponder]; 

关于iphone - 移动到另一个 View Controller 时,UISearchBar 不会在 resignFirstResponder 上隐藏键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8424848/

相关文章:

iphone - 在拖动时如何将 UISlider THUMB 直接移动/跳转到下一个值

iphone - 一个标签,两种不同的字体?

ios - 共享扩展帖子照片 : Failed to determine whether URL is managed by a file provider

ios - 如何在 swift 4 中重新排序单元格后保存 tableView 顺序

ios - 无法将 UISearchController 搜索栏添加到导航栏中,并且委托(delegate)方法未被调用

iphone - 将 UISearchBar 中的 BookmarkButton 替换为 Activityindicator

iphone - Objective-C 中的快速枚举(循环)如何工作? (即: for (NSString *aString in aDictionary). ..)

iphone - AFNetworking 2.0 的 POST 请求不工作,但在 HTTP 请求测试器中工作

ios - 导航回上一个 View Controller

ios - 在 AppDelegate 中自定义 UISearchBar 和 UINavigationBar 的外观?为什么要在类而不是实例级别进行定制?