简体   繁体   中英

Stop UISearchDisplayController from resigning first responder on searchbutton click

I've got a UISearchDisplayController and I want to avoid closing keyboard after search when clicking on the keyboard's search button. I tried to use the solution of Josh Cashwell from another Post: setting UISearchBarDelegate and implementing SearchBarShouldEndEditing . But I've got a problem to catch the right event: SearchBarShouldEndEditing shall return yes in any case (ie clicking anywhere else in view, dragging resulttableview,...) except when clicking search button.

My Implementation: I tried to set a variable in searchBarSearchButtonClicked and use it in searchBarShouldEndEditing to determine whether I shall return YES or NO . Works fine while iteratively tipping Searchtext and pressing search, but when clicking anywhere else in the view after pressing search, the keyboard does not close.

So hopefully there is a better way to check whether searchBarShouldEndEditing is called because of clicking the search button, but I don't get it.

Thanks so much for your help!!

Why do you want the keyboard to not go away when the user clicks the "Search" button in the keyboard?

But anyways... the way I would solve the problem is that in your searchBarShouldEndEditing: delegate method (before it returns YES ), make the text field that you want the keyboard to stay up for the new first responder (via UIResponder's becomeFirstResponder method). That way the keyboard wouldn't have a reason to dismiss.

Solved: Used an NSTimeInterval to save Time of pressing Searchbutton. On SearchBarShouldEndEdiing timedifference from now to the saved value is checked. If (diff < 0.001) {return NO;} else {return YES;} I don't know whether this is a smart solution but it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM