Search Bar click detect

北城余情 提交于 2019-12-11 03:42:50

问题


I have a search bar and i want to detect when the user click it , and after to disable a button while the user is editing in search bar. How can i do this , because i was trying to do wha you will see above but it's never called.

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
        NSLog(@"yes");
    }

Other methods like searchDisplayController are called.I set also

self.searchBar.delegate=self 

but no result.


回答1:


You can directly use the delegate of the UIsearchbar for getting the click event. you can use this delegate for it for checking the click. and for getting end editing this second one.

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    NSLog(@"Yes");
    return YES;
}

End Editing

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    return YES;
}



回答2:


The searchBarSearchButtonClicked: method is called when the user taps the "Search" (return) button on the keyboard. From you question, I understand that you want to detect when the user taps the search bar, to enter text for the search. If that's the case, you need to implement the searchBarShouldBeginEditing: or searchBarTextDidBeginEditing: methods in the UISearchBarDelegate. For example,

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    NSLog(@"yes");
    return YES;
}



回答3:


Swift version of the answer:

  1. Add UISearchBarDelegate to your view controller Assign its
  2. delegate to self searchBar.delegate = self Use
  3. searchBarShouldEndEditing or searchBarSearchButtonClicked

     func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool {
       return true
    }
    

or

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
}


来源:https://stackoverflow.com/questions/31154955/search-bar-click-detect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!