Skip to content

Instantly share code, notes, and snippets.

@i386
Created November 11, 2012 07:22
Show Gist options
  • Save i386/4054063 to your computer and use it in GitHub Desktop.
Save i386/4054063 to your computer and use it in GitHub Desktop.
Delegate implementor
//
// DSAppDelegate.m
// Autoexpand
//
// Created by James Dumay on 11/11/12.
// Copyright (c) 2012 James Dumay. All rights reserved.
//
#import "DSAppDelegate.h"
#import "DSValue.h"
@implementation DSAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
DSValue *value1 = [[DSValue alloc] initWithDisplayValue:@"AAA"];
DSValue *value2 = [[DSValue alloc] initWithDisplayValue:@"BBB"];
DSValue *value3 = [[DSValue alloc] initWithDisplayValue:@"CCC"];
DSValue *value4 = [[DSValue alloc] initWithDisplayValue:@"DDD"];
_values = [NSArray arrayWithObjects:value1, value2, value3, value3, value4, nil];
}
-(void)awakeFromNib
{
[_autocomplete setDelegate:self];
}
- (NSString *)tokenField:(NSTokenField *)tokenField displayStringForRepresentedObject:(id)representedObject
{
DSValue *value = representedObject;
return value.displayValue;
}
- (id)tokenField:(NSTokenField *)tokenField representedObjectForEditingString:(NSString *)editingString
{
for (int i = 0; i < _values.count; i++)
{
DSValue *value = [_values objectAtIndex:i];
if ([value isEqualTo:editingString])
{
return value;
}
}
return nil;
}
- (NSArray *)tokenField:(NSTokenField *)tokenField completionsForSubstring:(NSString *)substring indexOfToken:(NSInteger)tokenIndex indexOfSelectedItem:(NSInteger *)selectedIndex
{
for (int i = 0; i < _values.count; i++)
{
DSValue *value = [_values objectAtIndex:i];
if ([value.displayValue hasPrefix:substring])
{
return [[NSArray alloc] initWithObjects:value, nil];
}
}
return nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment