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.
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
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.
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