John Ballinger
John Ballinger

Reputation: 7540

Detect when a user clicks the paste button in a UITextView

I am having quite a issue trying to change the cut/copy/paste behavior of the UITextView. What I want to achieve is: detect when the user has pasted some text into the UITextView. When I detect this I will then check the data and do my thing.

According to the documents, I found out about UIResponder.

So I created an simple class that inherits UITextView. in the .m file I create 1 function called.

-(void) paste:(id)sender{
  NSLog(@"paste button was pressed do something");
}

But for some reason it never seems to fire. I could get the select statement working and tracing data.

-(void) select:(id)sender

1. Is this the correct way to detect Paste in a UITextView? 2. Right now I am tracking buy how many Characters UITextView changes and if its greater than one char then I suspect that it could be a paste operation. But since the iPhone can autocomplete words eg Wedn (goes to Wednesday) its possibly not a paste operation.

In Interface Builder, I selected my textView in my NIB file, and selected its "Class Identity" to my nearly created class before and I know that this file was working as a subclass but it just would not respond to the Paste event.

thanks.

Upvotes: 6

Views: 7130

Answers (4)

Aouiaiauo Eyjaajeyio
Aouiaiauo Eyjaajeyio

Reputation: 346

In case somebody still needs short and easy solution I'll share mine. I use wonderfull NSObject+REResponder category from REKit project. The solution is as easy as this:

- (void)hookMessageTextFieldPasteAction
{
    void (^textFieldDidPasteBlock)(id receiver, id sender) = ^(id receiver, id sender)
    {
        NSLog(@"paste hook");
    };
    [self.messageTextField respondsToSelector:@selector(paste:) withKey:nil usingBlock:textFieldDidPasteBlock];
}

Upvotes: 2

Alin
Alin

Reputation: 568

Same thing happens to me too. Select (and copy sometimes) gets called but paste never.

I have simulated the behavior using textViewDidChange where I always verify the difference between the current text and the previous one. If there are more letters different and the text.length is bigger than it must have been pasted.

Hope Apple will fix this.

Upvotes: 3

lillian
lillian

Reputation:

UITextView has a view that handles the cut, copy, paste. It's UIWebDocumentView. So if UITextView is the first responder, UIWebDocumentView will get it first instead of your implementation. I would like to overwrite these functions so this is very frustrating.

Upvotes: 3

Brock Woolf
Brock Woolf

Reputation: 47292

Yes your above code for paste is correct according to Apple's Documentation which refers to it here.

- (void)paste:(id)sender

I suspect you are implementing it in the wrong file. You should be implementing it in a file that is part of the 1st Responder chain. Have you subclassed the UITextView or are you using a vanilla one in your ViewController?

Hmmm I think the problem is that you may need to make your UITextView subclass become the delegate in order for the delegate method to work, because it's not by the looks of things. I'll try to find how i did that before.

Okay think i found it. I think you need to do this on your subclassed UITextField class:

@interface mySpecialTextFieldClass : NSObject <UITextFieldDelegate>
{

}

Adding that onto the end should work, give it a try! What it does is makes your subclass become an object of a type that the delegate can send a notification to...it's polymorphism, unless someone wants to correct me here :)

One last thing to try John, in the ViewController which contains the IBOutlet to the UITextView, try calling this method on the instance of the UITextView:

[myUITextView setDelegate:self];

Just to throw something completely random in there try setting it to become first responder.

[myUITextView becomeFirstResponder];

This is called programming in the dark kiddies! And is a very bad way to program, but I admit I do it and sometimes it pays off :P lol.

Upvotes: 1

Related Questions