UIMenuController不显示子视图UITextView

时间:2013-03-14 00:00:20

标签: ios objective-c uimenucontroller autocorrect

我正在使用SLTextField + Autocomplete,我更改为使用textview而不是字段以使其成为多行。由于某种原因,默认代码可以工作,但是当我移动到TextView时,它不再在superview中显示UIMenuController。代码附在下面,数据源在初始化的superview中设置,匹配的查找工作正常,菜单不会在视图中显示。将第一个响应者分配给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

2 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

目标rect应该基本上是您希望显示菜单的上方/下方/旁边的视图框。如果您没有为宽度和高度提供自定义值,该怎么办?

尝试更改此内容:

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

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