5

내 앱은 UISearchDisplayController을 사용합니다. 사용자가 검색어를 입력하면 검색 창에 계속 표시되기를 원합니다. 사용자가 일치하는 결과 중 하나를 선택했지만 사용자가 '검색'버튼을 클릭하지 않으면이 방법이 작동합니다.UISearchDisplayController가 비활성 상태 일 때 검색어를 계속 표시합니다.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     NSString *selectedMatch = [self.searchMatches objectAtIndex:indexPath.row]; 
     [self.searchDisplayController setActive:NO animated:YES]; 
     [self.searchDisplayController.searchBar setText:selectedMatch]; 

     return; 
    } 
    ... 

을하지만 -searchBarSearchButtonClicked:에서 같은 일을 할 경우 텍스트가 검색 창에 남아 있지 않습니다

이 작동합니다. 이 상황에서 내가 이것을 어떻게 달성 할 수 있을지에 대한 아이디어가 있습니까?

관련하여 검색 바의 텍스트를 설정했지만 (UISearchDisplayController은 비활성 상태로 유지) searchResultsTableView의 표시를 트리거합니다. 사용자가 검색 창을 탭했을 때만 표시하고 싶습니다.

편집 : 해결 언제든지 searchResultsTableView을 표시하지 않고 검색 창의 텍스트를 설정하는 찾았

// This hacky YES NO is to keep results table view hidden (animation required) when setting search bar text 
[self.searchDisplayController setActive:YES animated:YES]; 
[self.searchDisplayController setActive:NO animated:YES]; 
self.searchDisplayController.searchBar.text = @"text to show"; 

더 나은 제안이 여전히 환영!

답변

7

실제로 searchMatches 배열에서 올바른 요소를 선택하기위한 indexPath가 없으므로 searchBarSearchButtonClicked 메서드 내에서 동일한 코드를 사용할 수 없습니다.

사용자가 검색 버튼을 클릭하고 searchController 인터페이스를 숨기려면 검색에 사용할 텍스트를 알아야합니다 (예 : 목록에서 가장 일치하는 결과 선택).

사용자가 검색 버튼을 클릭 할 때이 예를 바로 검색어를 볼 수 있고 변경되지 않은 상태로 유지하자 :이 도움이

-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
    NSString *str = searchBar.text; 
    [self.searchController setActive:NO animated:YES]; 
    self.searchController.searchBar.text = str; 
} 

희망, 수동 검색 창에 문자열 일부를 트리거 재설정 빈센트

+0

아 감사합니다! 나는'[self.searchController setActive : NO animated : YES];를하고 있었다. self.searchController.searchBar.text = searchBar.text;'분명히'searchBar.text'는 검색 막대를 비우는'setActive' 메쏘드 때문이라고 생각했습니다. –

3

을 UISearchDisplayDelegate 메서드를 다시 호출하십시오. 이 경우에 당신이 원하지 않는 것입니다. 내가 수정할 것

조금 대답 vdaubry, 그것은 나에게 제공합니다

-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
    NSString *str = searchBar.text; 
    [self.searchController setActive:NO animated:YES]; 
    self.searchController.delegate = nil; 
    self.searchController.searchBar.text = str; 
    self.searchController.delegate = self //or put your delegate here if it's not self! 
}