我正在使用SLTextField +自動完成,我改爲使用textview而不是字段,以使其成爲多行。出於某種原因,默認代碼可以工作,但是當我轉移到TextView時,它不再在超級視圖中顯示UIMenuController。代碼如下所示,數據源在初始化時設置在超級視圖中,匹配的查找工作正常,菜單不會顯示在視圖中。將第一響應者分配給UITextView的功能與預期相同(根據UIMenuController的要求)。UIMenuController不從子視圖顯示UITextView
//
// 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
它在一年前,你能回答你自己的問題了嗎? – franck 2014-04-16 07:53:25