ios - 将功能绑定(bind)到键盘中的 "Search"按钮

标签 ios objective-c uitextfield

我有一个 UITextField 来搜索在那里输入的单词。我将 returnKeyType 设置为 UIReturnKeySearch。但是我不知道如何将我创建的函数绑定(bind)到键盘上的“搜索”按钮。

self.searchWord.returnKeyType=UIReturnKeySearch;

我该怎么做?谢谢。

最佳答案

如果我理解正确,您需要实现 UITextfield 委托(delegate):

在你的头文件中

@interface MyViewController : UIViewController <UITextFieldDelegate>

在viewDidLoad中

- (void)viewDidLoad {
    [super viewDidLoad];
    self.searchTextField.delegate = self;
}

然后实现委托(delegate)方法。

- (BOOL)textFieldShouldReturn:(UITextField *)aTextfield {        
    // Only do search when user hits return key on the search textfield.
    if ([aTextfield isEqual:self.searchTextField]) {
        // Method that does the search.
        [self doSearch];
    }

    return YES;
}

关于ios - 将功能绑定(bind)到键盘中的 "Search"按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18312462/

相关文章:

objective-c - 动态填充 iPhone 中的数据

iphone - 另一个 NSInternalInconsistencyException,但使用 NSFetchedResultsController

ios - 检测一个点是否在 MKPolygon 叠加层内

iphone - 在静态 TableView 的 UITextfield 中禁用编辑 - 仅限 iOS 6.0

ios - Rx swift : How to create cache for last network response without creating class/struct property?

ios - 如何在自定义 UISlider 中在 slider 值 0 之前和 slider 值 100 之后提供填充

ios - 阿拉伯文本的 URL 解析错误

objective-c - 在 NSTableView 中显示二维数组

ios - 立即用 UIPickerView 选项填充 UITextField

xcode - 如何在 UIKeyboardTypeDecimalPad 中包含返回按钮?