ios 7 - 自定义UIMenuItem无法在TableViewCell上运行

时间:2014-04-10 13:32:40

标签: 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];

然后添加其代表

// 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];
    }
}

//对表格视图单元格进行子类化

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

- (BOOL)canBecomeFirstResponder {
    return YES;
}

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

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

1 个答案:

答案 0 :(得分:1)

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;
}

试试这个让我知道......