UISearchBar implemented with Storyboards

落花浮王杯 提交于 2019-12-07 20:36:57

问题


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

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.

Thanks in advance.


回答1:


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.

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

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

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.



来源:https://stackoverflow.com/questions/9897171/uisearchbar-implemented-with-storyboards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!