複数行にするために、フィールドの代わりにテキストビューを使用するように変更した SLTextField+Autocomplete を使用しています。何らかの理由でデフォルトのコードは機能しますが、TextView に移動すると、スーパービューに UIMenuController が表示されなくなります。コードは以下に添付されています。データソースは初期化時にスーパービューに設定され、一致の検索は正しく機能しますが、メニューはビューに表示されません。UITextView へのファーストレスポンダの割り当ては、(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