ios 7 - 自定义 UIMenuItem 不适用于 TableViewCell

标签 ios uitableview uimenucontroller

我正在努力在 tableViewCell 上添加自定义 UIMenuItem。我使用了这个 stackoverflow post添加 customMenuItem。这在 ios 6 上运行良好。但它在 ios 7 上根本不起作用。

下面是我的实现:

viewDidLoad中:

UIMenuItem *sendByEmailMenuItem = [[UIMenuItem alloc] initWithTitle:@"Send By Email" action:@selector(sendByEmail:)];
[[UIMenuController sharedMenuController] setMenuItems: @[sendByEmailMenuItem]];
[[UIMenuController sharedMenuController] update];

然后添加它的委托(delegate)

// Shared Menu item delegate actions

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
    self.orderAtIndex = [self.orders objectAtIndex:indexPath.row];
    [self becomeFirstResponder];
    return YES;
}

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender 
{
    return  (action == @selector(sendByEmail:));
}


- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    if (action == @selector(sendByEmail:)) {
        [self sendByEmail:sender];
    }
}

//子类化表格 View 单元格

-(BOOL) canPerformAction:(SEL)action withSender:(id)sender {
    return (action == @selector(sendByEmail:));
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void) sendByEmail: (id) sender {
    // Some actions...
}

我做错了什么?任何帮助表示赞赏。谢谢

最佳答案

viewWillAppearviewDidLoad 中,我添加了这些

 UIMenuItem *translateToMenu = [[UIMenuItem alloc] initWithTitle:@"Translate to.." action:@selector(translateTo:)];
 UIMenuController *menuController = [UIMenuController sharedMenuController];
 [menuController setMenuItems:[NSArray arrayWithObject:translateToMenu]];
 [menuController setMenuVisible:YES animated:YES];

添加了这个方法

-(void) translateTo: (id) sender {}

并且只添加这两个方法

- (BOOL) canPerformAction:(SEL)selector withSender:(id) sender {
    if (selector == @selector(translateTo:)) 
        return YES;
    else
        return NO;
}

- (BOOL) canBecomeFirstResponder {
    return YES;
}

试试这个,让我知道……

关于ios 7 - 自定义 UIMenuItem 不适用于 TableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22989563/

相关文章:

ios - 将 UITableViewCell 恢复到正常编辑模式

ios - 在 Xcode Beta 6 之后快速使用 NSPredicate 崩溃

ios - xcode7 launchScreen.storyboard无法添加图像

ios - Swift : Trying to convert MIDI to Audio File 中的 AudioUnitRender 和 ExtAudioFileWrite 错误 -50

xcode - 设置具有正确详细信息的自定义单元格样式,但它不显示详细信息文本标签?

ios - iPadOS:防止 UIContextMenuInteraction 在不使用指针时触发

ios - 将设备 token 存储在 AppDelegate 中,然后在 ViewController 中使用它

ios - 自动尺寸不起作用

ios - 如何在 UIMenuItem 中显示多行?

iphone - 更改 View 后 UIMenuController 未出现