UIMenuController在第一次保持重置

时间:2016-03-03 20:37:24

标签: ios objective-c xcode uiwebview uimenucontroller

在UIWebView中选择文本时,我使用以下代码在UIMenuController中显示自定义项目。

//Custom UIMenuController actions for highlighting and commenting
    UIMenuItem *highlightItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Highlight", nil) action:@selector(highlightAction:) image:[UIImage imageNamed:@"HighlightIcon"]];
    UIMenuItem *commentItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Comment", nil) action:@selector(createCommentAction:) image:[UIImage imageNamed:@"CommentIcon"]];

    [UIMenuController sharedMenuController].menuItems = @[highlightItem, commentItem];
    [[UIMenuController sharedMenuController] update];

它第一次正常工作并且只显示我拥有的两个自定义选项,但之后每当选择一些文本时,它首先显示本机选项,然后在下一页项目中显示自定义选项。

我想知道如何永久删除原生选项 - 所有其他问题和示例似乎都不适用于iOS 7 +。

我也有这个用于启用菜单:

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(highlightAction:) ||
        action == @selector(createCommentAction:) ||
        return YES;
    else if (action == @selector(copy:))
    {
        return NO;
    }

    return [super canPerformAction:action withSender:sender];
}

#pragma mark - privates
- (void)pressme:(id)sender
{
    [[UIMenuController sharedMenuController] setTargetRect:[sender frame] inView:self.view];
    [[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
    [[UIMenuController sharedMenuController] update];
}

1 个答案:

答案 0 :(得分:0)

要禁用默认项目,请在方法canPerformAction:withSender:中而不是调用super canPerformAction,只需返回NO。

这是一个简单的例子,它应该如何用于一个动作(只是尝试过它,它起作用):

// Let say we have textField property outlet

// Action to display menu
- (IBAction)showMenu:(id)sender {
    UIMenuItem *item = [[UIMenuItem alloc] initWithTitle:@"test" action:@selector(test:)];
    [UIMenuController sharedMenuController].menuItems = @[item];
    [[UIMenuController sharedMenuController] update];

    [self.textField becomeFirstResponder];
    UIMenuController *theMenu = [UIMenuController sharedMenuController];
    [theMenu setTargetRect:self.textField.frame inView:self.textField.superview];
    [theMenu setMenuVisible:YES animated:YES];
    [[UIMenuController sharedMenuController] update];

}

// Enable only "test:" selector here, disable others
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(test:))
        return YES;
    return NO;
}

enter image description here