Search code examples
iphoneobjective-cxcode4autocompletetextfield

Obj C - Autocomplete


Hey I got a problem with a tutorial I found for autocompletion: It says -

Now, when the text in the text field has changed (ie: in the EditingChanged handler), just call the GetSuggestions method, which will return an array of strings that match the parameter passed

NSArray *suggestions = [autocomplete GetSuggestions:textField.text];

You can do whatever you want with these suggestions, I displayed them in a table view and when the user taps a cell, the text is added to the text field (source)

Well I placed the array in

...textField:(UITextField *)textField shouldChangeCharactersInRange...

but I cannot figure out how to 'call' the suggestions when I write something right behind my text somewhat like autocorrection :/ any thoughts?

tried something like

if ([textField.text length] > 0){
        ..somehow call suggestions

    }

but that just doesn't work :P


Solution

  • You need to create a view to display them in. All you have done so far is load the autocomplete suggestions in an array.

    You need to display them in a TableView or something. Look here to get a good tutorial on how they work. -> http://www.youtube.com/watch?v=_Qbjwx0hB6A&list=SPE83F832121568D36&index=13

    Take a good look at the source code provided by the author, he demonstrate how he use the array to populate a TableView.

    Edit: The youtube tutorial is for a NSTableView for OS X programming, but you get the hang of it.