1

I have a UITextview delegate - (BOOL)textView:(UITextView *)textView that is never getting called and I have been struggling with this for a couple of days to no avail. Here is what I have

I have declared the <UITextViewDelegate> in my header file and in ViewDidLoad I am setting the delegate, I have tried self.myTextView.delegate = self; and myTextView.delegate = self; along with wiring up the delegate in the NIB file instead of declaring it programmatically.

I have a few custom buttons that insert the text into my UITextView by 2 different methods depending on the button, the first method here is...

-(IBAction)string1:(id)sender {

NSRange range = myTextView.selectedRange;  
NSString * firstHalfString = [myTextView.text substringToIndex:range.location];  
NSString * secondHalfString = [myTextView.text substringFromIndex: range.location];  
myTextView.scrollEnabled = NO;  

NSString * insertingString = [NSString stringWithFormat:@"insert my text string"];

myTextView.text = [NSString stringWithFormat: @"%@%@%@",  
                 firstHalfString,  
                 insertingString,  
                 secondHalfString];  
range.location += [insertingString length];  
myTextView.selectedRange = range;  
myTextView.scrollEnabled = YES;}

and the second method is ...

-(IBAction)string2:(id)sender {

myTextView.text = [myTextView.text stringByAppendingString:@"my other string value"];}

With NSLog turned on, I can see that

- (void)textViewDidChange:(UITextView *)textView{
 NSLog(@"something changed");}

does get called but this delegate never gets called

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSUInteger newLength = (textView.text.length - range.length) + text.length;
if(newLength <= MAX_LENGTH)
{
    return YES;
} else {
    NSUInteger emptySpace = MAX_LENGTH - (textView.text.length - range.length);
    textView.text = [[[textView.text substringToIndex:range.location] 
                      stringByAppendingString:[text substringToIndex:emptySpace]]
                     stringByAppendingString:[textView.text substringFromIndex:(range.location + range.length)]];
    return NO;
}
NSLog(@"something else changed");}

At the top of my .M file I have defined #define MAX_LENGTH 100

Any help would be very much appreciated, my overall goal is to limit the number of charters in the UITextView.

Dan
  • 398
  • 4
  • 15
  • One first note, you won't see `NSLog(@"something else changed");`because both `if` and `else` do a `return` before they get there. :-) – ott-- Jan 18 '12 at 20:55
  • Yes, I am aware of that, I had the logging prior to the returns but still no luck. – Dan Jan 18 '12 at 21:00

1 Answers1

1

UITextView only sends textView:shouldChangeTextInRange:replacementText: to its delegate in response to user events. It does not send that message when your program sets the myTextView.text property.

I'm surprised that the UITextView is sending textViewDidChange: to the delegate, since the documentation says "This method is not called in response to programmatically initiated changes."

So, you could clip the text in textViewDidChange: instead (and rely on its behavior being different from the documentation), or modify and string1: and string2: to clip the text.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • you definitely helped me out pointing me in the right direction. I have chosen to limit the textview input by doing this `if (myTextView.text.length >= 10) { myTextView.text = [myTextView.text substringToIndex:10]; } NSLog(@"limit reached");` – Dan Jan 18 '12 at 21:29