1

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
4

2 回答 2

0

目标矩形本质上应该是您希望在其上方/下方/旁边显示菜单的视图框架。如果您不为宽度和高度提供自定义值怎么办?

尝试改变这个:

[completionMenu setTargetRect:CGRectMake(self.bounds.origin.x,self.bounds.origin.y,70,70) inView:self.superview];

对此: [completionMenu setTargetRect:self.frame inView:self.superview];

于 2014-11-20T08:44:22.603 回答
0

改变这个

[completionMenu setTargetRect:CGRectMake(self.bounds.origin.x,self.bounds.origin.y,70,70) inView:self.superview];
[self becomeFirstResponder];

对此

[completionMenu setTargetRect:CGRectMake(self.frame.origin.x,self.frame.origin.y,70,70) inView:self];
于 2013-03-15T09:09:27.600 回答