如何在点击键盘上有关 UISearchBar 的搜索按钮时关闭键盘?

发布于 2024-12-22 00:01:58 字数 191 浏览 0 评论 0原文

我正在开发一个应用程序,其中添加一个控件 UISearchBar。当我开始在 UIsearchBar 中编辑文本时,键盘会在屏幕上显示动画。当我完成完整的编辑或取消所有文本后,我将停留在关闭键盘的位置。 如何在点击 UIKeypad 中的搜索键时关闭键盘?

UITextField 和 UITextView 也有同样的问题吗?

谢谢

I m developing an application in which i adding one control UISearchBar. When i started editing text in UIsearchBar then keypad is animated on the screen. After i completed my complete editing or canceling all text then i will stuck on point of dismissing keypad.
How i dismiss keyboard on tapping search key form UIKeypad?

Also same question for UITextField and UITextView?

Thanks

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

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

发布评论

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

评论(3

阳光的暖冬 2024-12-29 00:01:58

确保具有 SearchBar 的视图控制器实现了 SearchBarDelegate 并将 searchBar.delegate 设置为 self:

@interface AddressSearchViewController : UIViewController <UISearchBarDelegate>

然后实现以下方法:

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

这将使当您点击键盘或搜索栏上的搜索按钮时键盘消失

Make sure the view controller which has the SearchBar implements the SearchBarDelegate and you set the searchBar.delegate to self:

@interface AddressSearchViewController : UIViewController <UISearchBarDelegate>

then implement the following method:

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

This will make the keyboard disappear when you tap the search button on the keyboard or the search bar

柏林苍穹下 2024-12-29 00:01:58

对于搜索栏:

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
   [searchBar resignFirstResponder];
}

对于文本字段也是如此:

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
}

For the search bar:

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
   [searchBar resignFirstResponder];
}

And same thing for the text field:

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
}
九公里浅绿 2024-12-29 00:01:58

在 ViewDidLoad 中使用此代码

-(void) ViewDidLoad
 {
    [super ........];
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [self.view addGestureRecognizer:gestureRecognizer];
    gestureRecognizer.cancelsTouchesInView = NO;    
 }

 - (void) hideKeyboard 
 {
    [texfieldname1 resignFirstResponder];
    [texfieldname2 resignFirstResponder];
 }

Use this code in ViewDidLoad

-(void) ViewDidLoad
 {
    [super ........];
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [self.view addGestureRecognizer:gestureRecognizer];
    gestureRecognizer.cancelsTouchesInView = NO;    
 }

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