2

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.

pajevic
  • 4,607
  • 4
  • 39
  • 73

2 Answers2

1

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!

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
  • You were absolutelly correct Carl. Fore some reason the delegate was synthesized in MyTextView.m, which it never occurred to me to look for since we don't declare a delegate in this class. I believe this must have been a copy/paste issue from another class. It's just interesting that there seem to be an inconsistency in Cocoa classes regarding when they use properties and instance variables. So sometimes you get away with it and sometimes you don't. That caused the confusion for me. – pajevic Aug 31 '12 at 08:18
  • Perhaps UIScrollView doesn't make its delegate public to its superclasses so they are forced to refer to it strictly through the property. – Carl Veazey Aug 31 '12 at 09:32
0

I have not tried this, but it might have something to do with protocols?

In that case, the third answer might help: How to use custom delegates in Objective-C

Community
  • 1
  • 1
Sti
  • 8,275
  • 9
  • 62
  • 124
  • Well, I am not making a custom delegate, so the question is not that relevant for me. And I do know how to use delegates; I have used them tons of times (I'm fairly experienced in Cocoa/Cocoa Touch development). But I haven't encountered the behavior I am describing in my question before. – pajevic Aug 31 '12 at 05:51