设计 iOS 搜索栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6203472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Designing iOS SearchBar
提问by Srikar Appalaraju
I want to have a simple SearchBar in ObjectiveC. Using UISearchBar
or UISearchBarDelegate
is confusing me. I could have used a UITextField
but it does not have the look & feel of a search bar.
我想在 ObjectiveC 中有一个简单的 SearchBar。使用UISearchBar
orUISearchBarDelegate
使我困惑。我本可以使用一个,UITextField
但它没有搜索栏的外观和感觉。
As in the image attached, I want just the searchbar no UITableView
associated with it. The image has a TableView attached but you get the point. Also after someone enters text into the searchBar & pressed "enter" how do I retrieve the text?
如所附图片所示,我只想要UITableView
与它无关的搜索栏。该图像附加了一个 TableView,但您明白了。另外在有人在搜索栏中输入文本并按下“输入”后,我该如何检索文本?
UPDATE:I am aware of these links which discuss the same, but they are more in light with using tables.
更新:我知道这些讨论相同的链接,但它们更适合使用表格。
http://blog.webscale.co.in/?p=228
http://blog.webscale.co.in/?p=228
http://ved-dimensions.blogspot.com/2009/02/iphone-development-adding-search-bar-in.html
http://ved-dimensions.blogspot.com/2009/02/iphone-development-adding-search-bar-in.html
How to implement search bar in iPhone?
回答by bensnider
Just make your view controller implement the UISearchBarDelegate
. In your xib file, all you need to do is to add a UISearchBar to your view and configure it as necessary, create an outlet for it (optional really but helps to be explicit), and assign the delegate outlet to your view controller.
只需让您的视图控制器实现UISearchBarDelegate
. 在您的 xib 文件中,您需要做的就是将 UISearchBar 添加到您的视图并根据需要对其进行配置,为其创建一个插座(确实是可选的,但有助于明确),并将委托插座分配给您的视图控制器。
Then, to respond to the search bar events, implement the UISearchBarDelegate
protocol methods as necessary. For example:
然后,为了响应搜索栏事件,UISearchBarDelegate
根据需要实现协议方法。例如:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[self handleSearch:searchBar];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
[self handleSearch:searchBar];
}
- (void)handleSearch:(UISearchBar *)searchBar {
NSLog(@"User searched for %@", searchBar.text);
[searchBar resignFirstResponder]; // if you want the keyboard to go away
}
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {
NSLog(@"User canceled search");
[searchBar resignFirstResponder]; // if you want the keyboard to go away
}