我正在努力在 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...
}
我做错了什么?任何帮助表示赞赏。谢谢
最佳答案
在 viewWillAppear
或 viewDidLoad
中,我添加了这些
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/