Gopinath
Gopinath

Reputation: 5442

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;
}

Upvotes: 3

Views: 1313

Answers (2)

Gopinath
Gopinath

Reputation: 5442

I have solved my problem. I have used below codes to solve.

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(copy:))
    {
        [self selectAll:self];

        return YES;
    }
    else if (action == @selector(cut:))
    {
        return NO;
    } 
        return NO;
}


- (void)copy:(id)sender 
{
    UIPasteboard *pastBoard = [UIPasteboard generalPasteboard];
    [pastBoard setString:self.text];
    self.selectedTextRange = nil;
    [self resignFirstResponder];
}

Thanks to Mr.Vimal Venugopalan and Mr.Mrueg. It is working for me. It will help to some one.

Upvotes: 1

Vimal Venugopalan
Vimal Venugopalan

Reputation: 4091

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

Upvotes: 0

Related Questions