在失去对uisearchbar的关注时辞职键盘

Tot*_*mus 7 cocoa-touch objective-c uisearchbar uisearchbardelegate

我正在我的应用程序的导航栏中创建一个UISearchBar选项.我的应用包含多个视图和子视图.我有这个主视图,其他3个观点.其中一个是空的(现在),另外两个有关于它们的表格视图.

当我正在进行实际搜索或者在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];
    }
    
    Run Code Online (Sandbox Code Playgroud)

我似乎无法做的是让它回应我的2个表格中的一个.或者当我重新填充主视图以包含完全不同的东西时.

我曾尝试更改touchesbegan方法以在触摸tableviews时重新调整搜索栏,但它到目前为止还没有工作.

亲爱的朋友先生,我已经尝试了其他几件事.谷歌,但这似乎是我需要的其他东西.

任何人都有任何想法,我可以做些什么来解决这个问题?

编辑:看来这样,当使用断点时,touchesbegan-method会响应backgroundview但当我触摸其中一个tableviews或导航栏(包含uisearchbar)时它不会响应.

Tot*_*mus 13

解决了!

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

我开始在我的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];
}
Run Code Online (Sandbox Code Playgroud)

然后当我在searchBar外面点击/点击/触摸时,我首先重新设置键盘,这是第一个响应者仍然是在我为2个子视图重新设置userInteraction之后.这样做的顺序至关重要!

这段代码允许你在一个mainViewController中将键盘重新签名,即使它挤满了大量的子视图.