2013-03-14 42 views
1

我正在使用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 
+0

它在一年前,你能回答你自己的問題了嗎? – franck 2014-04-16 07:53:25

回答

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]; 
+0

這似乎並沒有解決這個問題。 Self是UITextView,所以我假設它不想在本身內部顯示菜單?我應該將選擇器移到superview(一個普通的UIViewController),然後看看self.superview是否工作?它之前沒有。 – 2013-03-15 18:31:42

0

目標矩形基本上應是視圖的框架上/以下/門旁要顯示的菜單。如果您不給寬度和高度的自定義值,會怎麼樣?

嘗試修改此:

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

這樣: [completionMenu setTargetRect:self.frame inView:self.superview];