隐藏UIMenuController显示"粘贴""选择""全选"

时间:2014-08-26 08:23:29

标签: ios

我正在开发支持IM的应用。下面是UITextField,向上是UITableView,显示气泡中的历史消息(UIView)。 Bubble添加了longPressGesture,UIMenuController由longPressGesture调用。我的问题是当textField是 firstResponder 时,键盘显示,然后由longPressGesture调用UIMenuController,通常它工作正常。但是如果textField中有文本,则menuController会显示比我预期更多的项目,例如“select”,“selectAll”。

以下是我的代码:

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(copyMenuAction:)||
        action == @selector(deleteMenuAction:)||
        action == @selector(resendMenuAction:)||
        action == @selector(forwardMenuAction:))
    {
        return YES;
    }
   //    else if (action == @selector(cut:)||
   //             action == @selector(copy:)||
   //             action == @selector(paste:))
   //    {
   //        return NO;
   //    }
    else
        return [super canPerformAction:action withSender:sender];

    return  NO;
   }

  -(BOOL) canBecomeFirstResponder{
    return YES;
   }

1 个答案:

答案 0 :(得分:0)

尝试仅在UITableView中添加手势识别器。

UILongPressGestureRecognizer *longGes = [[UILongPressGestureRecognizer alloc] 
  initWithTarget:self action:@selector(handleLongPress:)];
longGes.minimumPressDuration = 2.0; //seconds
longGes.delegate = self;
[self.tableView addGestureRecognizer: longGes];

然后在手势处理程序中:

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
    CGPoint p = [gestureRecognizer locationInView:self.tableView];

    NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p];
    if (indexPath == nil) {
        NSLog(@"long press on table view but not on a row");
    }
    else { 
        if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
            NSLog(@"long press on table view at row %d", indexPath.row);
        }
        else {
            NSLog(@"gestureRecognizer.state = %d", gestureRecognizer.state);
        }
    }
}

希望这会有所帮助.. :)