UISearchBar 用 Storyboards 实现 [英] UISearchBar implemented with Storyboards

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

问题描述

我对 iOS 开发很陌生,但很兴奋.

I am very new to iOS development, very excited though.

我已经构建了一个应用程序,它使用故事板并使用 plist 文件的内容填充 UITableView.到目前为止,我设法让一切运行良好,但现在我想添加一个与联系人应用程序中的搜索栏非常相似的搜索栏(基本上这就是我的应用程序,只是填充了公司目录).我一直在这里和那里阅读,有一种非常简单的方法可以通过设置搜索栏的委托和数据源来做到这一点,但是我找不到任何用故事板演示这一点的教程.这听起来可能很愚蠢,但由于委托和数据源由故事板应用程序中的界面构建器更完整地处理,我无法弄清楚如何为这个简单的键入时"搜索连接搜索栏.

I have built an app the uses storyboards and populates a UITableView with the contents of a plist file. I managed to get everything running great so far, but now I want to add a search bar much like the one in the contacts app (essentially that's what my app is, just filled with a company directory). I keep reading here and there that there is a very simple way to do this by setting the delegate and data source for the searchbar, but I have not been able to find any tutorials that demonstrates this with storyboards. It may sound silly but since delegation and datasources are handled more completely by interface builder in a storyboard app, I can't figure out how to hook up the searchbar for this simple "as you type" search.

如果有人知道一个好的资源,或者如果代码很容易在这里发布,我真的很感激.几个小时以来,我一直在用我的头撞这个,我开始感到疯狂.

If anyone knows of a good resource, or if the code is easy to post here, I would REALLY appreciate it. I have been banging my head against this one for hours and I am starting to feel crazy.

提前致谢.

推荐答案

我也遇到了这个问题,因为关于如何使用 .xib 执行此操作的 Web 信息太多,而没有关于较新故事板的信息.我的解决方法是打开辅助编辑器,确保您可以并排看到故事板和视图控制器的 .h/头文件.

I hit this snag too with too much web information on how to do this with .xib's and no information on the newer storyboards. How I solved it was opening up the assistant editor, make sure you can see the storyboard and your view controller's .h / header file side by side.

转到 Storyboard 查看器,按住 Control 键并单击并将搜索字段拖到 .h 头文件的 @interface 区域(是的,直接拖到代码视图中).在弹出窗口中,为其命名(如searchBar"),它实际上会在您的代码中创建一个插座连接,并且应如下所示...

Go to the Storyboard viewer, holding Control and click and drag the search field into the @interface area of the .h header file (yes, right into the code view). On the popup, give it a name (like 'searchBar'), it essentially creates an outlet connection in your code and should look like the following...

@interface EmployeesTableViewController : UITableViewController
    @property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@end

(它也应该自动合成 .m 文件中的 searchBar.)

(It should also automatically synthesize that searchBar in the .m file, too.)

完成后,通过 .xib 跳过 UITableViewDelegate/UITableViewDataSource 和 File's Owner 内容,本视频教程的其余部分 http://www.youtube.com/watch?v=IqDZHgI_s24 详细介绍了如何对搜索框的其余部分进行编码以过滤表格视图.

With that wired up, skipping over the UITableViewDelegate/UITableViewDataSource and File's Owner stuff via the .xib, the rest of this video tutorial http://www.youtube.com/watch?v=IqDZHgI_s24 goes into great detail on how to code the rest of the search box to filter a table view.

这篇关于UISearchBar 用 Storyboards 实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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