views:

544

answers:

3

Hi,

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.

+1  A: 

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.

Brock Woolf
Hello, I subclassed the UITextView and I know that subclass was working buy implementing some of the other methods. I had the paste code in my subclass as well. But this does not seem to work (well in the simulator).
John Ballinger
I updated my answer, but the Simulator shouldn't make any difference just so you know. And gave you +1 btw :P
Brock Woolf
Hello again, I used this as you suggested to have the UITextFeildDelegate.@interface BSTextView : UITextView <UITextFieldDelegate>--- here is something pretty random ---if I implement the "select" method, this works for a "select"- (void)select:(id)sender{ NSLog(@"I want to select");}but still no love from either the device or the simulator if I press paste. I think this is actually a BUG...
John Ballinger
Hmm, you may be right. The Cut, Copy, Paste is only very new and it is possible that you need to do something else in order to get it working. There must be a way though, Apple wouldn't just ship a method with no way of invoking it.
Brock Woolf
I have tried the setDelegate as well. The code "half" works because "select" works as expected. So it does sort of seem that Paste just wont fire, which is very strange. I think I might fire a Radar at this.
John Ballinger
+2  A: 

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.

Hi Lillian, did you ever get this working?
John Ballinger
Sorry. no luck... Hopefully Apple will open it up for us.
+1  A: 

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.

Alin
This is exactly what I do as well. But be warned things like Wednesday will auto complete and give you false readings. so be careful and cause you textview.length to increase by more than 1.
John Ballinger