I have a Searchbar that generally works. The problem comes when I test on the iPhone, the autocorrect kicks in. I have the search bar set that if there is text in the query bar > 4 in length,then it starts putting things into the searchResults array. If there is a suggestion from autocorrect and I ignore it and select something from the array, the app crashes. I traced the error to the fact that the autocorrected word is being selected before the row selection (but both get done), so the searchResults array I thought I was accessing is no longer valid.
For instance, I put in "demp" into the search bar. Autocorrect suggests "demo". Meanwhile, the search returns a bunch of results with "dempster" in it. I select the first. As I do this, the search re-searches under "demo" and clears the original array (keep in mind I look to a server for the searches so the array has not re-filled). Then the didSelectRowAtIndexRow method triggers and there's nothing in the array and it crashes.
Any thoughts? Thanks in Advance.
Since you are using a search bar there should be no autocorrection, people want to find what they're looking for, not what some dictionary tells them to find. Simply set
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
and problem will be solved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With