简体   繁体   English

UITextField的带有搜索returnkey的resignFirstResponder

[英]resignFirstResponder for UITextField with search returnkey

Simple question: how can I resign my textfield if my "done" key is Search. 一个简单的问题:如果我的“完成”键是“搜索”,如何才能退出文本字段。 Essentially, what do I do if the user does not want to search, but instead wants to cancel... 本质上,如果用户不想搜索,而是想取消...该怎么办...

thanks 谢谢

You can use: a cancel button for SearchBar and need to implement this SearchBar delegate : 您可以使用:SearchBar的取消按钮,并且需要实现此SearchBar delegate

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    isSearching = NO; //This is a flag which specifies if searching is going on or not.
    searchBar.text = @""; //Clears out search bar text
    [self resetSearch]; //Reset search resets all the flags and variables.
    [self.leadsTable reloadData]; //Reloads the tableView
    [searchBar resignFirstResponder]; //Resigns responder from Search bar
}

This is a proper way to resign the responder if user doesn't want to search. 如果用户不想搜索,这是一种辞职响应者的正确方法。

Look at how you can add an in-built Cancel button in UISearchBar. 看一下如何在UISearchBar中添加内置的“取消”按钮。 Check the property " Shows Cancel Button " (Red Arrow highlight) 检查属性“ 显示取消按钮 ”(红色箭头突出显示)

在此处输入图片说明

EDIT: 编辑:

STEP-1: Check conditionally whether your textField's text is blank? 步骤1:有条件地检查您的textField文本是否为空白? If so resignFirstResponder for the TextField. 如果是这样,请为TextField重新签名FirstResponder。 You need to set the Delegate to self for the UITextField using code: 您需要委派设置为self为的UITextField使用代码:

txtField.delegate = self;

Or you can do the same from the interface builder by connecting TextField's delegate to File's Owner . 或者,您可以通过将TextField的delegate连接到File's Owner从界面构建器中执行相同的操作。

STEP-2: You need to implement this delegate method. 步骤2:您需要实现此委托方法。

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
   if([textField.text isEqualToString:@""])
   {
        [textField resignFirstResponder];
   }
    return YES;
}

EDIT-1: 编辑1:

Now create a UIToolbar with one bar button labeled 'Cancel' 现在,用一个标记为“取消”的条形按钮创建一个UIToolbar

Once you have done that: 完成此操作后:

You can write an button click event for UIToolBar: 您可以为UIToolBar编写按钮单击事件:

-(IBAction)cancelClicked:(id)sender
{
    [txtField resignFirstResponder];
}

Once you have done that you can now just write: 完成后,您现在可以编写:

txtField.inputAccessoryView = toolbarOutlet;

Hope this helps you. 希望这对您有所帮助。

The text on the return button is irrelevant to the discussion. 返回按钮上的文字与讨论无关。 You want to know how to resign first responder without pressing the return button, and there are a few ways to do it. 您想知道如何在不按返回键的情况下辞职第一响应者,并且有几种方法可以做到这一点。

  1. Use the inputAccessoryView of the text field to display a separate cancel button in a toolbar above the keyboard. 使用文本字段的inputAccessoryView在键盘上方的工具栏中显示一个单独的取消按钮。
  2. Use a tap gesture recognizer on the field's superview to recognize when the user taps outside the field, and call [self.view endEditing:YES] (where self is your view controller). 在字段的超级视图上使用轻击手势识别器,以识别用户何时在字段外进行点击,然后调用[self.view endEditing:YES] (其中self是您的视图控制器)。 This will cause the first responder to resign. 这将导致第一响应者辞职。 (This is very finicky in a scroll view.) (这在滚动视图中非常挑剔。)
  3. Swap out the rightBarButtonItem of the current view controller for a cancel bar button item while editing, assuming you have a UINavigationBar on screen at the time. 假设您当时在屏幕上有UINavigationBar ,则在编辑时将当前视图控制器的rightBarButtonItem交换为取消栏按钮项。 When editing ends, swap back in the regular right bar button item, if any. 编辑结束后,请换回常规的右栏按钮项(如果有)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM