2012-07-10 54 views
3

我有這個問題UISearchDisplayController。我想在專門的視圖中使用它來處理搜索。對於我的控制器類,我使用了搜索欄,searchcontroller和tableviewcontroller的組合。 SearchViewController鏈接到.xibUISearchDisplayController:取消作爲解僱按鈕

@interface SearchViewController : UIViewController <UISearchBarDelegate, UISearchDisplayDelegate, UITableViewDelegate, UITableViewDataSource> { 
    NSSet *words; 
    UISearchDisplayController *searchController; 
    UISearchBar *searchBar; 
    PhraseTableViewController *phraseTableViewController; 
} 

只要SearchViewController出現時,搜索欄變得firstresponder和搜索因此活性。一切看起來和工作正常,有一個例外: 爲了回到以前的觀點,我想使用SearchBar的取消按鈕。現在問題來了:如果用戶在沒有進行搜索時觸摸底層tableview的灰色區域,取消按鈕(並因此返回的唯一方法)消失。

這是我一直在努力,解決這一事:

  1. showsCancelButton = YESsearchDisplayControllerDidEndSearch。工程,但由此產生的UI很糟糕:取消按鈕轉換出來,然後重新出現。
  2. 在搜索控制器的searchResultTableView中添加一個UITapGestureRecognizer,希望能夠捕獲水龍頭。不起作用,它仍然通過。
  3. 子類UISearchBar並覆蓋self.searchController.searchBar setShowsCancelButton: - 沒有工作。

想法如何解決這個問題:

  1. 總是隱藏取消按鈕,而是添加自定義按鈕。問:你有一個想法如何做到這一點,使用戶界面看起來不錯?最令我關注的是處理所有這些情況,使得搜索文本框始終保持在按鈕旁邊。
  2. 溝渠UISearchDisplayController一起爲一個更靈活的解決方案。你知道在哪裏看? Pre-iOS 3這個控制器不存在,對嗎?周圍沒有解決方案仍然有效嗎?

我很高興爲您的任何輸入。提前致謝。

+0

你試過'searchDisplayControllerWillEndSearch'中的'showsCancelButton = YES'嗎? – Felix 2012-07-10 12:45:50

+0

是的,沒有效果。隱藏取消按鈕的事件必須在'willEndSearch'和'didEndSearch'之間激發。 – 2012-07-10 13:01:54

回答

1

好的我想我已經找到了一種可行的方法:我只是取消隱藏正確類型事件的導航欄,以便用戶獲得後退按鈕。請參閱下面的相關代碼,但是我必須補充一點,事件處理是針對UISearchDisplayController的其他「特殊」行爲的解決方案的一部分,這些行爲會導致導航欄在錯誤的時間重新出現(跳到細節頁面之前也會導致到醜陋的動畫)。

編輯:是的,我知道它很醜 - 但如果你問我,那該死的UISearchDisplayController也是如此。

static BOOL _cancelBtnClicked = NO; 
static BOOL _phraseClicked = NO; 

- (void)searchBarCancelButtonClicked:(UISearchBar *)_searchBar{ 
    if(self.searchController.active){ 
    _cancelBtnClicked = YES; 
    }else{ 
    [self back]; 
    } 
} 

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller{ 
    if(_cancelBtnClicked){ 
    _cancelBtnClicked = NO; 
    [self back]; 
    } 
    else if (!_phraseClicked) { 
    [self.navigationController setNavigationBarHidden:NO animated:YES]; 
    } 
} 

- (void)keyboardWillHide:(NSNotification *)notification { 
    if (_phraseClicked) { 
    _phraseClicked = NO; 
    } 
    else if(self.isViewLoaded && self.view.window != nil) { 
    self.navigationController.navigationBar.hidden = YES; 
    } 
} 

- (void)hideNavbarAndKeepHidden {   
    self.navigationController.navigationBar.hidden = YES; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 
} 

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    _phraseClicked = YES; 
    [self.phraseTableViewController tableView:tableView didSelectRowAtIndexPath:indexPath]; 
}