限制特定 UITextfield 的复制、粘贴选项

发布于 2024-10-04 14:24:03 字数 371 浏览 0 评论 0原文

我的 UIView 包含两个 UITextField。我需要限制一个文本字段的复制、粘贴选项。我不想限制另一个文本字段。

当我使用以下代码时,两个字段都受到复制、粘贴的限制。

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender 
{
    if ( [UIMenuController sharedMenuController] )
    {
            [UIMenuController sharedMenuController].menuVisible = NO;
    }
     return NO;
}

任何人都可以为我提供解决方案来解决我的问题。

My UIView contains Two UITextField.I need to restrict copy,paste option for one textfield.I don't want to restrict that for another.

When i am using the following code,Both the field gets restricted from copy,paste.

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender 
{
    if ( [UIMenuController sharedMenuController] )
    {
            [UIMenuController sharedMenuController].menuVisible = NO;
    }
     return NO;
}

Can any one provide me the solution to solve my problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

失而复得 2024-10-11 14:24:50

我有一个随机的想法,在文本视图上完美运行。没有理由它不能在文本字段上工作。

我将以下内容添加到我想要限制的文本字段中。

  • 长按手势识别器(1 次触摸)
  • 长按手势识别器(2 次触摸)
  • 点击手势识别器(2 次点击,1 次触摸)
  • 点击手势识别器(3 次点击,1 次触摸)
  • 点击手势识别器(1 次点击,2 次触摸)

然后分配以下内容对其进行编码。

- (IBAction)cancelTouch:(id)sender {
    //do nothing
}

我现在仍然可以滚动文本视图,但长按或双击现在绝对没有任何作用!

I had a random idea that worked perfectly on a text view. No reason why it wouldn't work on a text field.

I added the following to the text field I wanted to restrict.

  • Long Press Gesture Recognizer (1 touch)
  • Long Press Gesture Recognizer (2 touches)
  • Tap Gesture Recognizer (2 taps, 1 touch)
  • Tap Gesture Recognizer (3 taps, 1 touch)
  • Tap Gesture Recognizer (1 tap, 2 touches)

Then assigned the following code to it.

- (IBAction)cancelTouch:(id)sender {
    //do nothing
}

I can now still scroll through the textview but a long press or double tap now do absolutely nothing!

撩心不撩汉 2024-10-11 14:24:49

苹果的解释:

这个的默认实现
如果响应者方法返回 YES
类实现请求的操作
并呼叫下一个响应者,如果
没有。子类可以覆盖这个
启用菜单命令的方法
关于当前状态;例如,你
如果有的话将启用复制命令
是选择或禁用粘贴
如果粘贴板没有命令
包含正确的数据
纸板表示类型。

因此,解决方案是子类化 UITextView 并正确返回。

有关该方法的更多信息请参见此处

Explanantion from Apple:

This default implementation of this
method returns YES if the responder
class implements the requested action
and calls the next responder if it
does not. Subclasses may override this
method to enable menu commands based
on the current state; for example, you
would enable the Copy command if there
is a selection or disable the Paste
command if the pasteboard did not
contain data with the correct
pasteboard representation type.

So, the solution is to subclass the UITextView and return properly.

More information about the method here

菊凝晚露 2024-10-11 14:24:48

以下内容可防止粘贴任何长度超过 1 个字符的字符串。然而,1 个字符长的字符串将通过(可能对某些人有用 - 不需要子类化)。

首先给你的textField一个委托

myTextField.delegate = self; // OR [myTextField setDelegate:self];

然后将以下方法添加到你的ViewController

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

    if ( [string length] > 1) {
        return NO;
    }
    return YES;
}

The following prevents any string longer than 1 character to be pasted. String that is 1 character long will however get through (could be useful to some people - doesn't need subclassing).

First give your textField a delegate

myTextField.delegate = self; // OR [myTextField setDelegate:self];

Then add the following method to your ViewController

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

    if ( [string length] > 1) {
        return NO;
    }
    return YES;
}
弃爱 2024-10-11 14:24:43

创建 UITextField 的子类。在该子类中,实现

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (sel_isEqual(action, @selector(copy:))) {
        return NO;
    }
    return [super canPerformAction:action withSender:sender];
}

然后将此子类用于您不希望能够复制的字段,并使用常规 UITextField 作为您可以从中复制的字段。

Create a subclass of UITextField. In that subclass, implement

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (sel_isEqual(action, @selector(copy:))) {
        return NO;
    }
    return [super canPerformAction:action withSender:sender];
}

Then use this subclass for the field that you don't want to be able to copy in, and use a regular UITextField for the one that you can copy from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文