UISearchBar gotcha

I sort of discovered something quite by accident today.

I have had a persistent crash problem in my app for a long time, so I put in some code from the QuincyKit so that I could get the crash logs from devices when they encountered a problem. Unfortunately, the crash logs that I have been getting have been 99% unhelpful, as the only mention of my app is the main.m line shown at the bottom of the thread 0 stack trace.

In diagnosing a problem in another app today, it occurred to me that the problem in one of my apps could be related. My users have a UISearchBar on top of a table view of items, and they can enter some text to narrow down the item list. I of course am hooking into the textDidChange method in the UISearchBarDelegate protocol. However, at the direction of a specific customer with specific instructions, I discovered that if the auto correction parameter is turned on for the search bar, and some text is entered that has a suggestion below it, then the act of selecting a row from the filtered table view will cause the text in the search bar to be replaced with the suggested text, and then hilarity ensues as the textDidChange is fired again and the program returns a nil record instead of the one I just selected from the table view, since the table view switched in the textDidChange method.

So I corrected this issue in the original app, and it got the wheels turning. So I tried doing much the same thing on a device in my other app, and sure enough, my exception breakpoint in Xcode fires! (I think I heard some angels singing right about this time.) And just for good measure, I set it up so that it does not crash anymore in that instance, it just sits there and does nothing, which apparently is better than a crash.

Needless to say, from now on I am going to set all of my UISearchBar items to have the auto correction parameter set to No.

BTW, happy birthday to Michael Anthony of Chickenfoot fame. Oh, and he was in some band called Van Halen for a while too.

EDIT: I knew that my Spidey sense was detecting something familiar when I was putting together this post. Maybe I should read my own blog every once in a while…

Search bar and correction don’t play nice together

Leave a Reply