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.