iphone - 在 UITableView 中点击单元格时显示 UIMenuController 时出现问题

标签 iphone objective-c uitableview uimenucontroller

当用户长按分组 UITableView 中的单元格时,我尝试显示自定义 UIMenuController。但是,在成功检测到长按后,我似乎无法显示 UIMenuController。任何帮助是极大的赞赏。

MyViewController.h
@interface MyViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
  UITableView *table;
  @property (nonatomic, retain) IBOutlet UITableView *table;
@end

在 cellForRowAtIndexPath 中,我附加了长按手势识别器

cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SectionsTableIdentifier] autorelease];
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    [cell addGestureRecognizer:longPress];
    [longPress release];

这是我的handleLongPress操作方法

-(void)handleLongPress:(UIGestureRecognizer *)longPress {

  if (longPress.state == UIGestureRecognizerStateBegan) {

    CGPoint pressLocation = [longPress locationInView:self.table];
    NSIndexPath *pressedIndexPath = [self.table indexPathForRowAtPoint:pressLocation];

    UIMenuItem *first = [[UIMenuItem alloc] initWithTitle:@"Save" action:@selector(saveRecent)];
    UIMenuItem *second = [[UIMenuItem alloc] initWithTitle:@"Edit" action:@selector(editQuery)];

    UIMenuController *menuController = [UIMenuController sharedMenuController];
    menuController.menuItems = [NSArray arrayWithObjects:first,second,nil];

    [menuController setTargetRect:longPress.view.frame inView:longPress.view.superview];
    [menuController setMenuVisible:YES animated:YES];
    [pressedIndexPath release];
  }
}

编辑和保存的操作方法仅显示 UIAlertView。我还实现了以下方法,以确保显示 UIMenuController 时仅显示“保存”和“编辑”选项

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {

 BOOL canPerform = NO;

 if (action == @selector(saveRecent)) {
    canPerform = YES;
 }
 if (action == @selector(editQuery)) {
    canPerform = YES;
 }

 return canPerform;
}

我还声称 MyViewController 是第一响应者

-(BOOL)canBecomeFirstResponder {

  return YES;
}

最佳答案

我相信您需要有一个声明firstResponder状态的 View 才能显示UIMenuController。我在您的代码中没有看到这种情况发生。

我写了使用 UIMenuController 的说明作为这个问题的答案:

Customize UIMenuController

关于iphone - 在 UITableView 中点击单元格时显示 UIMenuController 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4258839/

相关文章:

ios - 解析iOS PFFile下载顺序

ios - 滚动时 UITableView 不刷新

iphone - 网络在模拟器上工作在 iPhone 设备上不起作用

iphone - 在另一个UIView中多次添加相同的UIView

objective-c - 什么是 "non-weak zeroing reference"

iphone - iOS - RSS 阅读器库?

iphone - 如何从外部框架加载资源

iphone - 覆盖表情符号图形

iphone - UITableView中的删除按钮

ios - 我将如何创建一个状态消息,该消息会短暂显示 "above"UITableView(如 UIRefreshControl)?