UITextViewDelegate methods not being called (UITextView within UITableViewCell)
Asked Answered
O

2

14

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!

Odometer answered 27/9, 2012 at 17:29 Comment(3)
Do you actually set your view controller as the delegate of the text view? Something like myTextView.delegate = myDetailViewController...Oden
I did NOT actually set my view controller as the delegate! THANK YOU!Odometer
@Odometer : Add following code in viewdidload [noteTxtView.delegate self];Peep
M
31

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.

Marylyn answered 27/9, 2012 at 18:30 Comment(1)
Amazing. I forgot to set the delegate. THANK YOU!Odometer
F
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.

Fixate answered 19/5, 2016 at 16:39 Comment(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.Landgraviate

© 2022 - 2024 — McMap. All rights reserved.