Search code examples
objective-ccocoa-touchuitextviewuiscrollviewdelegateuitextviewdelegate

UIScrollViewDelegate methods not working with a UITextView subclass


I have a class MyTextView which inherits from UITextView. I also have a MyViewController which is a subclass of UIViewController. MyViewController implements the UITextViewDelegate protocol and is set as the delegate of MyTextView.

MyViewController implements some of the delegate methods from the UITextViewDelegate (like - (void)textViewDidChange:(UITextView *)textView) and they all work fine. However, when I try to implement - (void)scrollViewDidScroll:(UIScrollView *)scrollView or some or the other scroll delegate methods they do not get called. It is my understanding that this should work since UITextView inherits from UIScrollView and UITextViewDelegate conforms to UIScrollViewDelegate which declares the scroll methods in question.

The strange thing is that if I go into IB and change the class of the text view from MyTextView to UITextView all the delegate methods get called, including the scrolling onces.


Solution

  • It's possible you re-synthesized the delegate property, which will mean there is now a new instance variable private to your class, and synthesized accessors pointing to that private variable. So any superclass implementations that refer to the delegate by instance variable rather than the property will not get through (those referring to the property will invoke your subclasses accessors still, of course). Looks like maybe UITextField uses the property and UIScrollView uses the instance!