在 uisearchbar 上失去焦点时放弃键盘

发布于 2024-12-05 08:50:26 字数 949 浏览 5 评论 0原文

我正在我的应用程序的导航栏中创建一个 UISearchBar 选项。 我的应用程序由多个视图和子视图组成。 我有这个主要观点,其中对他自己还有另外 3 个观点。其中一个是空的(目前),另外两个有表格视图。

我希望我的键盘在我搜索时显示,并在我进行实际搜索或触摸/单击 uisearchbar 外部时隐藏。 我根据需要使用 searchbardelegate 。

我可以通过以下方式使用 [searchBar resignFirstResponder] 隐藏键盘。

  • 当我按下返回键时。
  • 当我手动取消搜索时
  • 当我按任意键盘按钮进行搜索或取消时。
  • 当我使用触摸屏幕的空白部分时

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {    
        UITouch *touch = [[事件 allTouches] anyObject];
        if ([mySearchBar isFirstResponder] && [触摸视图] != mySearchBar) {
            [mySearchBar 辞职第一响应者];
        }
        [超级触摸开始:触摸事件:事件];
    }
    

我似乎无法做的是使其响应触摸我的两个表格视图之一。或者当我重新填充主视图以包含完全不同的东西时。

我尝试过更改touchesbegan方法来在触摸表格视图时放弃搜索栏,但到目前为止还没有奏效。

我已经尝试了我亲爱的朋友先生发现的其他一些东西。谷歌,但这一切似乎都不是我需要的。

有人知道我可以做什么来解决这个问题吗?

编辑: 看起来,当使用断点时,touchesbegan-method确实响应backgroundview,但当我触摸tableview或navigationbar(包含uisearchbar)时它不响应。

I'm making an UISearchBar option in the navigationbar of my app.
My app consists of multiple views and subviews.
I have this mainview which has 3 other views on himself. one of it is empty (for now) the other 2 have tableviews on them.

I want my keyboard to show when I'm searching and hide when i'm doing the actual search or when i touch/click outside the uisearchbar.
Im using the searchbardelegate as is required.

Im able to hide the keyboard using [searchBar resignFirstResponder] in the following ways.

  • When im pressing the return key.
  • When i cancel search manually
  • When i press any of the keyboard buttons to search or cancel.
  • When i touch an empty part of the screen using

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {    
        UITouch *touch = [[event allTouches] anyObject];
        if ([mySearchBar isFirstResponder] && [touch view] != mySearchBar) {
            [mySearchBar resignFirstResponder];
        }
        [super touchesBegan:touches withEvent:event];
    }
    

What i not seem to be able to do is make it respond to touching one of my 2 tableviews. Or when im refilling the mainview to contain something different entirely.

Ive tried changing the touchesbegan method to resign the searchbar when touching the tableviews but it hasnt worked so far.

I've tried several other things found by my dear friend mr. google, but it all seems to be something other then I need.

Anyone have any ideas of what I might do to fix this problem?

EDIT:
It appears so that, when using breakpoints, touchesbegan-method does respond to the backgroundview but it doesnt respond when i touch either of the tableviews or the navigationbar (containing the uisearchbar).

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

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

发布评论

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

评论(2

岁月如刀 2024-12-12 08:50:26

解决了!

- (BOOL) searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    [self.myViewController1.customView1 setUserInteractionEnabled:NO];
    [self.myViewController2.customView2 setUserInteractionEnabled:NO];   
    [searchBar setShowsCancelButton:YES animated:[mySettings animation]];
    return YES;   
}

我首先关闭了 2 个子视图上的 userInteraction,此时我开始使用 searchBar。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{    
    UITouch *touch = [[event allTouches] anyObject];
    if ([self.mySearchBar isFirstResponder] && [touch view] != self.mySearchBar) 
    {
        [self.mySearchBar resignFirstResponder];
        [self.myViewController1.customView1 setUserInteractionEnabled:YES];
        [self.myViewController2.customView2 setUserInteractionEnabled:YES];
    }
    [super touchesBegan:touches withEvent:event];
}

然后,当我在搜索栏外部单击/点击/触摸时,我首先退出仍然是第一响应者的键盘,之后我为 2 个子视图重新设置 userInteraction。
执行此操作的顺序至关重要!

这段代码允许您在单个 mainViewController 中放弃键盘,即使它挤满了大量子视图。

Solved it!

- (BOOL) searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    [self.myViewController1.customView1 setUserInteractionEnabled:NO];
    [self.myViewController2.customView2 setUserInteractionEnabled:NO];   
    [searchBar setShowsCancelButton:YES animated:[mySettings animation]];
    return YES;   
}

I started by shutting down the userInteraction on my 2 subviews, at the moment I start using the searchBar.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{    
    UITouch *touch = [[event allTouches] anyObject];
    if ([self.mySearchBar isFirstResponder] && [touch view] != self.mySearchBar) 
    {
        [self.mySearchBar resignFirstResponder];
        [self.myViewController1.customView1 setUserInteractionEnabled:YES];
        [self.myViewController2.customView2 setUserInteractionEnabled:YES];
    }
    [super touchesBegan:touches withEvent:event];
}

Then when I click/tap/touch outside the searchBar I first resign the keyboard which is first responder still AND AFTER that I set userInteraction back on for the 2 subviews.
The order of doing this is vital!

This piece of code allows u to resign the keyboard in a single mainViewController even when it is crowded with a massload of subviews.

红墙和绿瓦 2024-12-12 08:50:26

你尝试过吗,

UISearchBar *searchBar;

接下来设置UISearchBar的Getter和Setter属性。

并调用any方法

[searchBar resignFirstResponder];

Have you tried this,

UISearchBar *searchBar;

Next set the Getter and Setter Property of UISearchBar.

and call the any method

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