I am using SLTextField+Autocomplete which I changed to use a textview instead of field in order to make it multiline. For some reason the default code works, but when I move over to TextView it no longer shows the UIMenuController in the superview. The code is attached below, the datasource is set in the superview on initialization and the finding of matches works correctly, the menu just will not display in the view. Assigning first responder to the UITextView is functioning as expected (as per the requirement of UIMenuController).
//
// SLTextField+Autocomplete.m
// TMSTaxi
//
// Created by Laurent Spinelli on 13/08/12.
// Copyright (c) 2012 Elemasoft. All rights reserved.
//
#import "SLTextField+Autocomplete.h"
@implementation SLTextField_Autocomplete
@synthesize completionMenu;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
completionMenu = [UIMenuController sharedMenuController];
}
return self;
}
- (void)dealloc
{
}
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
NSString *sel = NSStringFromSelector(action);
NSRange match = [sel rangeOfString:@"magic_"];
if (match.location == 0) {
return YES;
}
return NO;
}
- (BOOL) canBecomeFirstResponder
{
NSLog(@"HERE");
return YES;
}
- (void)showAutocompleteItems:(NSString*)_string
{
[self becomeFirstResponder];
NSMutableArray* menuItems = [[NSMutableArray alloc] init];
NSInteger counter = 0;
for (NSString* value in self.dataSource) {
if ([value rangeOfString:_string options:NSCaseInsensitiveSearch].location == 0 ) {
NSString *sel = [NSString stringWithFormat:@"magic_%@", value];
[menuItems addObject:[[UIMenuItem alloc] initWithTitle:[value capitalizedString] action:NSSelectorFromString(sel)]];
counter ++;
}
if (counter >= SLTextFieldMaxItemToDisplay) {
break;
}
}
[completionMenu setTargetRect:CGRectMake(self.bounds.origin.x,self.bounds.origin.y,70,70) inView:self.superview];
[self becomeFirstResponder];
[completionMenu setMenuItems:menuItems];
[completionMenu setArrowDirection:UIMenuControllerArrowDown];
NSAssert([self becomeFirstResponder], @"Sorry, UIMenuController will not work with %@ since it cannot become first responder", self);
[completionMenu setMenuVisible:YES animated:YES];
//[self performSelector:@selector(doShowMenu) withObject:nil afterDelay:0.5];
}
- (void)doShowMenu
{
}
- (void)tappedMenuItem:(NSString *)_string {
self.text = [_string capitalizedString];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
if ([super methodSignatureForSelector:sel]) {
return [super methodSignatureForSelector:sel];
}
return [super methodSignatureForSelector:@selector(tappedMenuItem:)];
}
- (void)forwardInvocation:(NSInvocation *)invocation
{
NSString *sel = NSStringFromSelector([invocation selector]);
NSRange match = [sel rangeOfString:@"magic_"];
if (match.location == 0) {
[self tappedMenuItem:[sel substringFromIndex:6]];
} else {
[super forwardInvocation:invocation];
}
}
@end