searchBar의 일반 텍스트 버튼에 액세스하는 방법은 무엇입니까? (How to access searchBar's clear text button?)


문제 설명

searchBar의 일반 텍스트 버튼에 액세스하는 방법은 무엇입니까? (How to access searchBar's clear text button?)

tableView 검색에서 "지우기" 버튼 (편집할 때 나타나는 버튼)을 눌렀을 때 키보드를 사라지게 하려고 했습니다. "지우기" 버튼을 클릭했을 때 어떻게 감지하여 firstResponder를 사임할 수 있습니까? 이미 textDidChange 메소드에서 이것을 시도했습니다:

if (SearchBar.text == @"") {
    [SearchBar resignFirstResponder];
    NSLog(@"clear called");
}

작동하지 않았고...또한 시도했습니다:

 if (SearchBar.text == nil) {
    [SearchBar resignFirstResponder];
    NSLog(@"clear called");
}

두 메소드 모두 호출되었음을 보여줍니다. 어떤 아이디어가 있습니까?

편집: 이제 registeredFirstResponder가 작동하지 않는 것 같습니다. 키보드가 화면에 유지됩니다. 내가 뭘 잘못하고 있니?


참조 솔루션

방법 1:

For string comparison you should use

if([SearchBar.text isEqualToString: @""])

방법 2:

You can try watching the text property of the search bar by registering for a KVO notification:

[self.searchBar addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:NULL];

and then implementing:

‑ (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  if (object == self.searchBar && [keyPath isEqualToString:@"text"]) {
    // Handle the new value of self.searchBar.text
  }
}

Edit: nevermind, answered above =)

방법 3:

I know this question is old, but another way to do this is:

if(searchText.length == 0)

in ‑ (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

(by sudo rm ‑rfKingofBlissCameron SpickertSteph Sharp)

참조 문서

  1. How to access searchBar's clear text button? (CC BY‑SA 3.0/4.0)

#uitableview #uisearchbar #iphone #first-responder #objective-c






관련 질문

레코드 삭제 중 오류 발생, 포착되지 않은 예외 'NSInternalInconsistencyException'으로 인해 앱 종료 (error while deleting records,Terminating app due to uncaught exception 'NSInternalInconsistencyException')

IOS 7에서 편집 상태의 셀을 반환하는 dequeueReusableCellWithIdentifier (dequeueReusableCellWithIdentifier returning cells in editing state in IOS 7)

UITableViewCell 탭 호버 (UITableViewCell tap hover)

ios swift tableview 셀 사이의 공간을 어떻게 변경할 수 있습니까? (How can I change the space between tableview cells ios swift)

테이블 뷰 셀을 선택할 때 뷰 컨트롤러를 푸시할 수 없습니다. (Can't push my view controller when I select a table view cell)

셀이 선택되면 UIImageView가 사라지지만 UILabel은 사라지지 않습니다. (UIImageView disappears but not UILabel when cell is selcted)

UITableView의 테두리를 설정하는 방법은 무엇입니까? (How to set a border for UITableView?)

plist에서 특정 데이터를 가져오는 UITableView(탐색 템플릿을 사용하지 않음) (UITableView (not using Navigation Template) getting specific data from plist)

searchBar의 일반 텍스트 버튼에 액세스하는 방법은 무엇입니까? (How to access searchBar's clear text button?)

'NSInternalInconsistencyException', 이유: '범위를 벗어난 섹션(0)에 대한 행 수를 요청했습니다.' ('NSInternalInconsistencyException', reason: 'Requested the number of rows for section (0) which is out of bounds.')

UITableViewCell에서 키보드 탭을 숨기는 UITapGestureRecognizer (UITapGestureRecognizer to hide keyboard taps on UITableViewCell)

특정 UITableViewCells에 고유한 삽입을 추가하는 방법은 무엇입니까? (How to add unique insets to specific UITableViewCells?)







코멘트