12

I must be doing something fundamentally wrong, my implementation methods for the UITextViewDelegate aren't firing. I have a UITextView that is a subview of a UITableCellView and the delegates aren't being called.

- (void)textViewDidBeginEditing:(UITextView *)textView {

    NSLog(@"textViewDidBeginEditing");
    // never called...
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    NSLog(@"shouldChangeTextInRange");  
    // never called... 
}

Suggestions? I don't know whether it matters, but the protocol is explicitly named in my @interface declaration.

@interface DetailViewController () <UITextViewDelgate, ..., ....>

Thanks!

ToddB
  • 2,490
  • 3
  • 23
  • 40

2 Answers2

30

You should add the textViewObj.delegate = self or give delegate connection for that text view property in xib file, then it should work and fire all delegate methods of UITextView.

Pugalmuni
  • 9,350
  • 8
  • 56
  • 97
Ganesh G
  • 1,991
  • 1
  • 24
  • 37
6

Couple of pointers on this based on my experience:

1) Check whether you are using a UITextField or a UITextView.

2) Based on the type of text field you are using add the following declaration to your interface declaration

@interface MyInterface : UIViewController<UITextFieldDelegate>
                      or
@interface MyInterface : UIViewController<UITextViewDelegate>

3)Register your controller as the delegate for your text field:

[myTextField setDelegate:self];

4)Override the desired method which being

 -(void)textFieldDidBeginEditing:(UITextField *)textField
              or
 - (void)textViewDidBeginEditing:(UITextView *)textView;

in my case.

Akhil Kateja
  • 139
  • 1
  • 4
  • 1
    Man you saved my life!! I have been pulling my hair out trying to get this delegate to fire. Then I read your comment 1) above. It turns out that UITextField and UITextView look a lot alike when you have been chasing a bug for hours. – Scooter May 31 '17 at 22:42