Hi, I have a small problem with a predictive text email field I'm trying to write.
It is sick (in a bad way), and here are the symptoms.
If my address book has the names David Smith, and Richard Smidgens, if I type in the characters "Sm", it behaves properly, seemingly, and shows both names, as options.
The problem, if you'll look closer, is that it's actually populated the field with the first two letters of the first name it's found.
If I type in a third letter "i", instead of it keeping both name (Smith/Smidgens), it actually adds the i, to the first two letters of the first name, giving me "Dai", and removing the two names from contention for predictability.
Any idea what I'm doing wrong here? Here's the code for this section
Any help would be greatly appreciated... thank you.
-Phil
---
It is sick (in a bad way), and here are the symptoms.
If my address book has the names David Smith, and Richard Smidgens, if I type in the characters "Sm", it behaves properly, seemingly, and shows both names, as options.
The problem, if you'll look closer, is that it's actually populated the field with the first two letters of the first name it's found.
If I type in a third letter "i", instead of it keeping both name (Smith/Smidgens), it actually adds the i, to the first two letters of the first name, giving me "Dai", and removing the two names from contention for predictability.
Any idea what I'm doing wrong here? Here's the code for this section
Any help would be greatly appreciated... thank you.
-Phil
---
Code:
/**
Creates the array of items used in the combo box of search results.
*/
- (NSArray*) createMatchesText: (NSArray*) searchResults
{
NSMutableArray* matchesArray = [NSMutableArray array];
size_t i;
for (i = 0; i < [searchResults count]; ++i)
{
id obj = [searchResults objectAtIndex: i];
NSString* first = 0;
NSString* last = 0;
NSString* email = 0;
@try
{
first = [obj valueForProperty: @"First"];
}
@catch (NSException* e)
{
}
@try
{
last = [obj valueForProperty: @"Last"];
}
@catch (NSException* e)
{
}
@try
{
id emails = [obj valueForProperty: @"Email"];
if (emails)
{
NSString* primaryId = [emails primaryIdentifier];
int index = [emails indexForIdentifier:primaryId];
email = [(ABMultiValue*)emails valueAtIndex: index];
}
}
@catch (NSException* e)
{
}
NSMutableString* str = [[NSMutableString alloc] init];
if (first)
{
[str appendString: first];
}
if (last)
{
[str appendString: [NSString stringWithFormat: @" %@", last]];
}
if (email)
{
[str appendString: [NSString stringWithFormat: @" <%@>", email]];
}
[matchesArray addObject: str];
}
return matchesArray;
}
- (NSArray *)tokenFieldCell: (NSTokenFieldCell*)tokenFieldCell
completionsForSubstring: (NSString*)substring
indexOfToken: (long)tokenIndex
indexOfSelectedItem: (long*)selectedIndex
{
// See if anything matches in the address book
ABAddressBook* ab = [ABAddressBook sharedAddressBook];
NSMutableArray* elms = [[NSMutableArray alloc] init];
ABSearchElement* firstName = [ABPerson searchElementForProperty:kABFirstNameProperty
label:nil
key:nil
value: substring
comparison: kABPrefixMatchCaseInsensitive];
ABSearchElement* lastName = [ABPerson searchElementForProperty:kABLastNameProperty
label:nil
key:nil
value: substring
comparison: kABPrefixMatchCaseInsensitive];
ABSearchElement* email = [ABPerson searchElementForProperty:kABEmailProperty
label:nil
key:nil
value: substring
comparison: kABPrefixMatchCaseInsensitive];
[elms addObject: firstName];
[elms addObject: lastName];
[elms addObject: email];
ABSearchElement* searchElm = [ABSearchElement searchElementForConjunction:kABSearchOr children:elms];
NSArray* searchResults = [[ab recordsMatchingSearchElement:searchElm] retain];
NSArray* matches = [self createMatchesText: searchResults];
if ([matches count] > 0) {
*selectedIndex = 0;
}
else {
*selectedIndex = -1;
}
return matches;
}
- (NSTokenStyle)tokenFieldCell: (NSTokenFieldCell *)tokenFieldCell styleForRepresentedObject: (id)representedObject
{
return NSPlainTextTokenStyle;
}
Last edited by a moderator: