在uisearchbar上失去焦点时辞职键盘 [英] resign keyboard when losing focus on uisearchbar

查看:105
本文介绍了在uisearchbar上失去焦点时辞职键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序的导航栏中创建一个UISearchBar选项。
我的应用程序包含多个视图和子视图。
我有这个主视图有自己的3其他意见。其中一个是空的(现在),另外两个有他们的表格。



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



无法使用[searchBar resignFirstResponder]以下列方式隐藏键盘。

$ b $
  • 当我手动取消搜索时

  • 当我使用

    触摸屏幕的空白部分时

    <$> p $ p> - (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];
    }




    能够做的是让它响应触摸我的2个表视图之一。



    我尝试改变touchesbegan方法,在触摸tableview时退出搜索栏,但是到目前为止它还没有工作。 p>

    我试过了我亲爱的朋友mr发现的其他几件事。 google,但这一切似乎都是我需要的东西。



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



    EDIT:
    当使用断点时,touchesbegan方法会响应backgroundview,但是当我触摸任何一个tableview或导航栏(包含uisearchbar)时, 。

    解决方案

    解决了问题!

       - (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];
    }

    然后当我点击/点击/触摸searchBar之外,我首先辞职这是第一个响应者仍然和以后我设置userInteraction为2个子视图。
    这样做的顺序是至关重要的!



    这段代码允许你在一个单独的mainViewController中辞职键盘,即使它拥有一个massload子视图。


    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).

    解决方案

    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.

    这篇关于在uisearchbar上失去焦点时辞职键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆