views:

724

answers:

2

Hi, I would like to have a UITextField or UITextView in where as I do some input, alternatives will appear something similar to when you type an address in Mail Application, alternatives appear down and is possible tap them so get a better input user interface.(since there is no need to type the complete word or address or phone number)

I do know how to fetch data from Address Book framework, also how to input text in UITextField/UITextView and its delegates but I don't know what kind of structure to use for fetching and showing data as the user do his/her input.

I know basic CoreData if this matters,

I hope I can get some help.

UPDATE (2010/3/10): I don't have problem make a native-like GUI but I am asking about the algorithm, does any body knows what kind algorithm is best for this thing? maybe some binary tree? Or should I just fetch data from coredata everytime?

Thanks

Ignacio

UPDATE (2010/03/28): I've been very busy these days, so I have not tried UISearchResults but it seems fine to me. BUT I wonder was there a necessity of deletion of the wining answer? I don't think is fair my reputation went down and couldn't see the winning answer. ;(

A: 

I was looking for the same thing a while ago. Something that people kept suggesting was the Three20 project (google it).

For my needs this was overkill because it requires the whole project to build and I didn't want the whole project. Plus it's more fun to try it yourself :)

I ended up starting from scratch and making my own:

I started out with a subclass of a UIScrollView to contain the different controls. I subclassed a UITextField and overrided "editingRectForBounds" to support multiple lines. The bit where the contacts are displayed is just a UITableView with a background color of:

[UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1];

And separator color:

[UIColor colorWithWhite:0.85 alpha:1];

This and the use of a shadow makes it looks like it's sunken slightly under the UITextField. I create the shadow with a custom UIView, loading it once and hiding it when required, but it works just as well with an image.

Finally, I made the blue pill shapes with a custom UIView which can intercept "touchesBegan" to know when they should change color.

Adding them is a simple matter of calculating where they need to go and using:

[myTextField addSubview:myBlueView];

Hope that helps!

Tom Irving
+4  A: 

You don't need some advanced algorithm to do this kind of thing... if you want to search the address book, then you can do so each time the user types in a character (or however frequent you need to seach). To do this, just take a look at the UISearchDisplayController class. I learned how to do almost the exact thing by looking at Apple's TableSearch sample app.

That app searches a list of objects using different fields (All, Device, Desktop, Portable)... so you could adapt it to Address Book fields (First Name, Last Name, Address...). The only thing you need to change is the search within the Address Book. I don't know exactly what your requirements ask for but this should be what you need to get it done. If you have any trouble with the code let me know... but this example really helped me, so hopefully it works for you.

Ryan Ferretti
Wow. Sometimes auto-accept doesn't do any justice (*most* of the time it is OK). I can't re-award the bounty, but I've deleted the "winning" answer that simply posted one of these links. Sorry that it went this way.
Marc Gravell
it's ok... thanks for your help Marc!
Ryan Ferretti