【问题标题】:UIMenuController keep resetting after first timeUIMenuController 在第一次后保持重置
【发布时间】:2016-03-03 20:37:23
【问题描述】:

在 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];

It works fine the first time and display only the two custom options I have, but after that whenever some text is selected it shows the native options first and the custom ones on the next page of items.

我想知道如何永久删除本机选项 - 所有其他问题和示例似乎不适用于 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];
}

【问题讨论】:

    标签: ios objective-c xcode uiwebview uimenucontroller


    【解决方案1】:

    要禁用默认项,在方法@​​987654323@ 中而不是调用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;
    }
    

    【讨论】:

    • 奇怪似乎对我不起作用。当我第二次突出显示某些内容时,它们仍然都出现在菜单中 - 复制、共享、定义等......
    • 我实际上是在使用它来突出显示 UIWebView 中的文本......也许这会导致差异?
    • 我遇到了问题。如果我在我的文本字段中写一些东西,然后选择文本,我可以看到默认菜单项。我是如何解决的 - 创建自定义类TextField : UITextField,并在这个类中覆盖canPerformAction:withSender:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    相关资源
    最近更新 更多