Search code examples
cocoa-touchcopyuitextviewuimenucontroller

Hide Copy and Deselect UITextView options after copying all text


I am working on a messaging app. I want to give a "copy" option to the user when they enter their message in a UITextView. When the user presses the "copy" button, it is copying the message, but the popover shows again and again, and the text is still selectable.

I don't know how to control this. I have pasted some source code for your reference.

I wrote a sub class for UITextView.

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    NSLog(@"Action : %@", NSStringFromSelector(action));
    NSLog(@"Sender : %@", sender);
    if (action == @selector(copy:))
    {
        [self selectAll:self];
        //return [super canPerformAction:action withSender:sender];
        return YES;
    }
    else if (action == @selector(cut:))
    {
        return NO;
    } 
        return NO;
}

Solution

  • If you are using the iOS5

    UITextView adopts the UITextInput protocol, which has a selectedTextRange property. Set the property to nil:

    Add the below code just above the last return NO.

    self.selectedTextRange = nil;
    

    Hope this helps