Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 失去对uisearchbar的关注时退出键盘_Objective C_Cocoa Touch_Uisearchbar_Uisearchbardelegate - Fatal编程技术网

Objective c 失去对uisearchbar的关注时退出键盘

Objective c 失去对uisearchbar的关注时退出键盘,objective-c,cocoa-touch,uisearchbar,uisearchbardelegate,Objective C,Cocoa Touch,Uisearchbar,Uisearchbardelegate,我正在应用程序的导航栏中创建UISearchBar选项。 我的应用程序由多个视图和子视图组成。 我有一个主要观点,对他自己有三个其他观点。其中一个是空的(目前),另外两个上面有TableView 我希望我的键盘在搜索时显示,在实际搜索或在uisearchbar外触摸/单击时隐藏。 我正在按要求使用searchbardelegate 我可以通过以下方式使用[searchBar resignFirstResponder]隐藏键盘 当我按回车键时 当我手动取消搜索时 当我按任何键盘按钮进行搜索或取消

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

我希望我的键盘在搜索时显示,在实际搜索或在uisearchbar外触摸/单击时隐藏。 我正在按要求使用searchbardelegate

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

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

    - (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];
    }
    
我似乎做不到的是让它对触摸我的两个桌面视图中的一个做出反应。或者当我重新填充mainview以包含完全不同的内容时

我尝试过改变touchesbeent方法,在触摸桌面视图时退出搜索栏,但到目前为止还没有成功

我试过我亲爱的朋友谷歌先生发现的其他一些东西,但似乎都是我需要的东西

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

编辑: 这样,当使用断点时,touchesbearth方法确实会响应backgroundview,但当我触摸TableView或navigationbar(包含uisearchbar)时,它不会响应

解决了

- (BOOL) searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    [self.myViewController1.customView1 setUserInteractionEnabled:NO];
    [self.myViewController2.customView2 setUserInteractionEnabled:NO];   
    [searchBar setShowsCancelButton:YES animated:[mySettings animation]];
    return YES;   
}
在我开始使用搜索栏的那一刻,我关闭了2个子视图上的用户交互

- (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];
}
然后,当我在搜索栏外单击/点击/触摸时,我首先退出键盘,它仍然是第一个响应者,然后我为这两个子视图重新设置userInteraction。 这样做的顺序是至关重要的

这段代码允许你在一个mainViewController中退出键盘,即使它挤满了大量的子视图。

你试过这个吗

UISearchBar *searchBar;
接下来设置
UISearchBar
的Getter和Setter属性

并调用any方法

[searchBar resignFirstResponder];