我正在实现自定义 UIMenuController
并试图找出答案。如何在 UITextfield
中合法地禁用 UIMenuController
的“复制”和“定义”UIMenuItems?文本字段不可编辑。我尝试使用以下方法禁用“复制”:
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(copy:))
{
return NO;
}
return [super canPerformAction:action withSender:sender];
}
- (IBAction)tapTextViewGesture:(id)sender {
UIMenuItem *myItem1 = [[UIMenuItem alloc] initWithTitle:@"myItem1" action:@selector(myItem1Pressed:)];
UIMenuItem *myItem2 = [[UIMenuItem alloc] initWithTitle:@"myItem2" action:@selector(myItem2Pressed:)];
UIMenuItem *myItem3 = [[UIMenuItem alloc] initWithTitle:@"myItem3" action:@selector(myItem3Pressed:)];
// Access the application's shared menu
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObjects:myItem1,myItem2,myItem3, nil]];
CGRect menuRect = CGRectMake(20, 50, 200, 0);
// Show the menu from the cursor's position
[menu setTargetRect:menuRect inView:self.view];
[menu setMenuVisible:YES animated:YES];
}
但是菜单仍然显示“Copy”和“Define”UIMenuItems
。我怎样才能禁用它们,只留下我的元素?
最佳答案
最后通过子类化 UITextView
解决了它(为它创建了自定义类)并添加了
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(copy:))
{
return NO;
}
return NO;
}
在我的自定义 TextView 子类的 .m 文件中。
之后“复制”不再出现,有或没有[menu update]
;
关于ios - 如何在 UITextfield 中禁用复制和定义 UIMenuController 的 UIMenuItems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27747140/