Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

autocorrect in UISearchBar interferes when I hit didSelectRowAtIndexPath

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.

like image 768
Nelson Ko Avatar asked Dec 22 '11 18:12

Nelson Ko


1 Answers

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.

like image 55
Eugene Avatar answered Oct 20 '22 14:10

Eugene