在iOS中,如何禁用粘贴到UITextfield或其他输入字段?

在iOS中,如何禁用粘贴到UITextfield或其他输入字段?,ios,objective-c,uitextfield,copy-paste,paste,Ios,Objective C,Uitextfield,Copy Paste,Paste,我知道这里列出的解决方案: 在这里: 但这并不是我想要做的。我觉得这有点混乱,因为用户仍然可以点击并按住文本字段,并看到粘贴选项可用。如果用户点击它,文本字段就不会响应该操作 我想从出现的选项列表中删除“粘贴”选项。可能吗?任何指示都将不胜感激。提前感谢。另一种方法是对UITextField进行子类化,并重写canperformation:withSender:method,如中所述 - (BOOL)textField:(UITextField *)textField shouldChang

我知道这里列出的解决方案:

在这里:

但这并不是我想要做的。我觉得这有点混乱,因为用户仍然可以点击并按住文本字段,并看到粘贴选项可用。如果用户点击它,文本字段就不会响应该操作


我想从出现的选项列表中删除“粘贴”选项。可能吗?任何指示都将不胜感激。提前感谢。

另一种方法是对UITextField进行子类化,并重写canperformation:withSender:method,如中所述

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    if(textField == txtCardNumber)
    {
        if([string length]>1){
            //Disable to paste action
            return NO;

        }
     }
}
使用下面的方法

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
     //Do your stuff
     [[NSOperationQueue mainQueue] addOperationWithBlock:^{
     [[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
     }];
     return [super canPerformAction:action withSender:sender];
}

第二个解决方案不管用吗?。。我的理解是,第二种解决方案允许您隐藏要隐藏的菜单项,甚至完全隐藏菜单。您发布的第一个链接的答案阻止粘贴选项出现在文本字段的菜单中。你试过了吗?@dasblinkenlight,第二种解决方案不符合我的要求。我试过了,但它根本没有隐藏菜单。我相信它应该完全隐藏菜单,但对我来说不是。那可能是因为我在测试版中工作,所以我必须注意这个选项。@rmaddy,我尝试了第一个选项,但也没有成功。它不会隐藏要粘贴的选项,而是仅禁用对粘贴事件的响应。用户仍然可以看到粘贴选项,这是我想要禁用的。在我自己的应用程序中使用
canpeformation:withEvent:
解决方案,它可以正确地阻止粘贴菜单出现。你一定是出什么事了。我建议你问一个新问题,为什么你的具体解决方案没有按预期工作。包括您正在使用的代码。
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
     //Do your stuff
     [[NSOperationQueue mainQueue] addOperationWithBlock:^{
     [[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
     }];
     return [super canPerformAction:action withSender:sender];
}