设计iOS SearchBar [英] Designing iOS SearchBar

查看:118
本文介绍了设计iOS SearchBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在ObjectiveC中有一个简单的SearchBar。使用 UISearchBar UISearchBarDelegate 让我感到困惑。我本可以使用 UITextField ,但它没有外观&感觉搜索栏。



如附图所示,我只想搜索栏中没有 UITableView 与之关联。图像附有TableView,但你明白了。在有人将文字输入searchBar&按下输入如何检索文本?





更新:我知道这些链接讨论相同的内容,但它们更适合使用表格。



http:// blog .webscale.co.in /?p = 228



http://ved-dimensions.blogspot.com/2009/02/iphone-development-adding-search-bar-in。 html



如何在iPhone中实现搜索栏?



UISearchBar示例代码



UITeViewBtroller中的UISearchBar?

解决方案

让你的视图控制器实现 UISearchBarDelegate 。在你的xib文件中,你需要做的就是在你的视图中添加一个UISearchBar并根据需要进行配置,为它创建一个插座(实际上是可选的,但有助于明确),并将代理插座分配给你的视图控制器。 / p>

然后,要响应搜索栏事件,请根据需要实施 UISearchBarDelegate 协议方法。例如:

   - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[self handleSearch:searchBar];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
[self handleSearch:searchBar];
}

- (void)handleSearch:(UISearchBar *)searchBar {
NSLog(@用户搜索%@,searchBar.text);
[searchBar resignFirstResponder]; //如果你想让键盘消失
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
NSLog(@用户取消搜索);
[searchBar resignFirstResponder]; //如果你想让键盘消失
}


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.

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?

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://ved-dimensions.blogspot.com/2009/02/iphone-development-adding-search-bar-in.html

How to implement search bar in iPhone?

UISearchBar Sample Code

UISearchBar in UITableViewController?

解决方案

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.

Then, to respond to the search bar events, implement the UISearchBarDelegate protocol methods as necessary. For example:

- (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
}

这篇关于设计iOS SearchBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆