Nishith Shah
Nishith Shah

Reputation: 523

How to restrict Copy, Paste for UITextField in iPhone?

i want to restrict the copy or Paste option for particular UITextfield in my Application.

Upvotes: 1

Views: 2432

Answers (3)

Aakil Ladhani
Aakil Ladhani

Reputation: 982

You can implement it like this:

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

Otherwise you can write:

- (BOOL)canBecomeFirstResponder {
    return NO;
}

To make your UITextField non-editable.

Upvotes: 2

Siva Charan
Siva Charan

Reputation: 18064

Create a subclass of UITextField.

In that subclass, implement

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (sel_isEqual(action, @selector(copy:))) //@selector(paste:) { 
        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.

Refer this URL for more info:-

iPhone – Disable the Cut/Copy/Paste Menu on UITextField

UIResponder

iPhone SDK Development

Upvotes: 2

Maulik
Maulik

Reputation: 19418

Add the following piece of code in the implementation file of the view controller which containing the UITextField

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

OR

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(paste:) // or @selector(copy:)
        return NO;
    return [super canPerformAction:action withSender:sender];
}

Upvotes: 5

Related Questions