2

我有一个带有 UISearchBar 的 tableView。在升级到 iOS 4 之前,当用户选择一个单元格并按下新的 viewController 时,键盘会随着表格滑到左侧的视图之外。

现在,它只是停留在新视图顶部的屏幕上。如果我调用 [mySearchBar resignFirstResponder]; 我可以摆脱键盘,但动画很尴尬,因为它在新视图被推送时从屏幕底部滑落。

有谁知道为什么 iOS 4 中的行为会有所不同?如何将键盘绑定到 tableView 以便在推送新视图时从屏幕上滑出?

谢谢!

编辑: 这里有一些额外的信息。我在导航栏后面有 uisearchbar。当用户按下按钮时它会滑出。我像这样添加 uisearchbar:

[self.navigationController.view insertSubview:self.mySearchBar belowSubview:self.navigationController.navigationBar];

我猜是因为导航栏实际上并没有被推送到下一个视图(尽管搜索栏确实如此),所以键盘保持在原位。

我尝试在 tableview 后面创建一个 uiview 并将搜索栏添加到其中,但由于 UINavigationController 和我的代码结构,我无法让它工作。

4

1 回答 1

1

就个人而言,我建议实现一个使用 UISearchDisplayController 的 SearchBar。我认为这实现了 xCode 使用的标准模板,并且非常流畅地处理所有响应者。这是一个方法示例,您可以尝试将其复制到代码中并使用

[self addSearchBar:@"enter search keywords here" hasScope:NO]

推荐代码:

- (void) addSearchBar:(NSString*)placeholder hasScope:(BOOL)aScope {


    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
    [searchBar sizeToFit];

    //searchBar.delegate = self;
    searchBar.placeholder = placeholder;
    self.tableView.tableHeaderView = searchBar;

    if (aScope==YES){
        searchBar.showsScopeBar = YES;
        searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"Flags",@"Listeners",@"Stations",nil];     
    }




    UISearchDisplayController *searchDC = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

    // The above assigns self.searchDisplayController, but without retaining.
    // Force the read-only property to be set and retained. 
    [self performSelector:@selector(setSearchDisplayController:) withObject:searchDC];

    //searchDC.delegate = self;
    //searchDC.searchResultsDataSource = self;
    //searchDC.searchResultsDelegate = self;

    [searchBar release];
    [searchDC release];

}
于 2010-07-30T07:03:44.697 回答