Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement a autocomplete textfield Objective C

I am starting to work with text fields, and I want to implement a functionality with a custom autocomplete.

In a lot of blogs and answer, implement it with UITables to show the autocomplete, but I do not need this, I need to create a autocomplete like a iPhone function, where show a pop out with the filter but only with my array of words.

For example.

my_array = @{"apple","banana","pear","orange","berry"}

When I type "app" the text field only need to show me "apple" but not complete the text in the text field, should show the pop up with the autocomplete.

Something like this but only with my array.

enter image description here

like image 449
user2720097 Avatar asked May 13 '14 02:05

user2720097


2 Answers

You can build this behaviour yourself using the UITextFieldDelegate methods ( implement the delegate in your UIView

@interface someViewController : UIViewController <UITextFieldDelegate> 

In doing this you get access to whatever the user has typed in

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

Where you can compare it to the items in your array and display another UIView or a custom button that when selected populates your text field.

Don't forget to tell your textfield who it's delegate should be, probably in your viewDidLoad method, but can also be done in the xib view

myTextField.delegate = self;

I know this seems laborious but it will be extremely gratifying. Here's the apple doc for the UITextViewDelegate

like image 145
Mindeater Avatar answered Nov 14 '22 14:11

Mindeater


I've implemented a custom auto complete textfield few days ago and the raywenderlich tutorial was helpful.

http://www.raywenderlich.com/336/auto-complete-tutorial-for-ios-how-to-auto-complete-with-custom-values

You probably need to implement your own table view to make it look like the screen you attached. The short tutorial will give you an idea how it should be done. I will post my code tomorrow if you want.

like image 42
HMHero Avatar answered Nov 14 '22 14:11

HMHero