将 UISearchBar 绑定到操作中

发布于 2024-10-05 05:57:47 字数 353 浏览 0 评论 0原文

从一开始就让你知道,我是 iPhone 开发的新手,我承担了一项艰巨的任务。

首先,我正在编写一个允许您在线购物的应用程序。我们在一个知名的在线购物网站上有一个附属帐户,正在尝试将我们的应用程序与搜索该网站结合起来,并将结果放入表格中,并带有指向该网站的超链接等。

但首先要解决的问题是,一次一件事:

在 Interface Builder 中,我有一个搜索栏和一个连接到搜索栏的插座。我还制作了一个 IBAction,只是为了获取输入到搜索栏中的文本,将其存储到变量中,然后搜索该网站(是的,这对于操作来说有很多内容)。

因为我对 IB 的这个过程还很陌生,所以如何将搜索栏与 IBAction 连接起来?我知道,愚蠢的问题。

To let you know from the beginning, I am a newbie at iPhone development, and I have taken an a monstrous task.

First of all, I am writing an application that allows to you to online shop. We have an affiliate account with a known online shopping website, are trying to tie our app into searching that site, and putting the results into a table, with hyperlinks to the site, etc.

But first problems first, one thing at a time:

In Interface Builder, I have a Search Bar, and an outlet connected to the search bar. I also have an IBAction made just to take the text that is entered into the search bar, storing it into a variable, and then searching this website (yes, that's a lot of stuff for an action).

Because I am so new at this process with IB, how do I connect the Search Bar with the IBAction? I know, stupid question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满身野味 2024-10-12 05:57:47

假设您没有特定的“搜索”按钮,请将您的视图控制器设置为 UITextField 的委托并执行您的操作,

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // Code here...
    return YES;
}

不要忘记在界面文件中声明协议

@interface ... : ... <UITextFieldDelegate>

一旦完成,每次按下 GO/SEARCH 键时在键盘上,它会调用上面的方法。

[编辑]
这是供您参考的苹果文档 http:// /developer.apple.com/library/ios/#documentation/UIKit/Reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html

Assuming you don't have a specific SEARCH button, set your View Controller as the delegate for UITextField and implement your action on

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // Code here...
    return YES;
}

Don't forget to declare the protocol in your interface file too

@interface ... : ... <UITextFieldDelegate>

Once that's done, everytime the GO/SEARCH key is pressed on the keyboard, it will call the method above.

[EDIT]
Here's the apple Doc for your reference http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html

岛徒 2024-10-12 05:57:47

您可以将视图控制器设置为 IB 中的搜索栏委托(或以编程方式),并实现 UISearchBarDelegate 协议来定义搜索栏的操作。

作为初学者,您应该熟悉委托模式,因为许多 iOS SDK 都依赖于它(iOS 应用程序编程指南 是一个很好的起点)。

更新

您需要在搜索栏的delegate 中实现UISearchBarDelegate 方法。例如,如果您的搜索栏是 MyViewController 的属性,您可能希望在 MyViewController 的实现中包含这一行:

[self.searchBar setDelegate:self];

就这么简单;现在,只要您在 MyViewController 中实现了这些方法,它们就会在相关操作发生时自动被调用。

You can set your view controller as the search bar's delegate in IB (or programmatically), and implement the UISearchBarDelegate protocol to define actions for your search bar.

As a beginner, you should familiarize yourself with the delegate pattern, since a lot of the iOS SDK relies on it (the iOS Application Programming Guide is a good place to start).

Update

You need to implement the UISearchBarDelegate methods in your search bar's delegate. For example, if your search bar is a property of MyViewController, you probably want to have this line in MyViewController's implementation:

[self.searchBar setDelegate:self];

It's as simple as that; now, as long as you have those methods implemented in MyViewController, they will automatically get called when the related action occurs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文