3

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;
}
jscs
  • 63,694
  • 13
  • 151
  • 195
Gopinath
  • 5,392
  • 21
  • 64
  • 97
  • can anyone please help me to solve this issue? Thanks in advance. – Gopinath Sep 14 '12 at 10:28
  • [this question](http://stackoverflow.com/questions/1920541/enable-copy-and-paste-on-uitextfield-without-making-it-editable) would help your cause.. if not then comment – Vimal Venugopalan Sep 14 '12 at 10:39
  • @Maulik Yes you are correct. Once we copied the text from UITextView by pressed Copy Button. This should be hide the Copy button and deselect all UITextview texts. Can you please help me? – Gopinath Sep 14 '12 at 10:43
  • @VimalVenugopalan Thanks for your link. I will check it and get back to you. – Gopinath Sep 14 '12 at 10:44
  • have u tried `textView.selectedTextRange = nil;` – CRDave Sep 14 '12 at 10:13
  • It is showing error: Assigning to 'NSRange' (aka 'struct _NSRange') from incompatible type 'void *'. But how can i hide copy option once the user pressed Copy button. Any idea? Thank you. – Gopinath Sep 14 '12 at 10:26

2 Answers2

1

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.

Gopinath
  • 5,392
  • 21
  • 64
  • 97
0

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

Vimal Venugopalan
  • 4,091
  • 3
  • 16
  • 25
  • Thanks for your answer and link. I got some more idea from there. I have solved my problem. Thanks again. – Gopinath Sep 14 '12 at 11:38
  • I accepted your answer. But i didn't used this way right. Could you please tell me how to use UITextView. K i will look this from Google. Anyway thank you friend. I hope you will help me in future. Thanks again. – Gopinath Sep 14 '12 at 11:42