Hide Copy and Deselect UITextView options after copying all text

南楼画角 提交于 2019-12-07 12:30:06

问题


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

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/12421821/hide-copy-and-deselect-uitextview-options-after-copying-all-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!