我正在尝试在我的项目中实现一个搜索栏。
我已经创建了 SearchBar socket ,设置了委托(delegate),但是当我按下任何键盘按钮时,此按钮的值不会出现在 UISearchBar
谁能告诉我为什么?
- (void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar {
// When the search button is tapped, add the search term to recents and conduct the search.
NSString *searchString = [aSearchBar text];
NSLog(searchString);
}
最佳答案
也许你忘了实现这个方法:
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
return YES;
}
通过返回 YES,您“说”您接受编辑。
编辑
你甚至不需要实现这个方法,默认情况下 UISearchBar 接受编辑。
不需要实现额外的方法。
为了确定我刚刚做了以下事情:
- 在 xib 文件中拖出一个搜索栏;
- 通过 xib 将委托(delegate)绑定(bind)到我的 View Controller ;
- 实现了 searchBarSearchButtonClicked: 并在其上放置了一个断点。
完美运行。确保您以正确的方式完成所有操作。
关于objective-c - 按键盘按钮时 SearchBar 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14059472/