2011-09-20 79 views
7

我正在我的應用程序的導航欄中創建一個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]; 
    } 
    

似乎什麼我不能夠做的就是它撫摸我的2個tableviews的一個迴應屏幕的空白部分。或者當我重新填充主視圖來完全包含不同的東西時。

我試着改變touchesbegan方法來在觸摸tableviews時退出搜索欄,但它迄今還沒有工作。

我嘗試了一些其他的東西,我的親愛的朋友先生髮現了。谷歌,但它似乎都是我需要的東西。

任何人有任何想法,我可以做什麼來解決這個問題?

編輯: 它似乎是這樣的,當使用斷點時,touchesbegan方法確實對backgroundview做出響應,但當我觸摸tableview或導航欄(包含uisearchbar)時,它不響應。

回答

13

解決了!

- (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子視圖設置用戶交互回。 這樣做的順序至關重要!

這段代碼允許你在一個mainViewController中放棄鍵盤,即使它擁擠在一個massload的子視圖中。

0

你有沒有試過,

UISearchBar *searchBar; 

下一組UISearchBar getter和setter屬性。

並調用任何方法

[searchBar resignFirstResponder]; 
相關問題