title: Why does respondsToSelector not work for me in this case?
body: <p>I have a little conundrum that is driving me right up the wall.  I am using delegation pretty heavily as a pattern in an application I am writing.  I'm trying to be as "careful" in the code calling the delegate as I can by testing the delegate with "[delegate respondsToSelector]" on each delegated call.  Everything works fine unless I am in a UIView subclass.  In that case, respondsToSelector returns NO yet I can safely call the delegate code so clearly it exists and works correctly.</p>
  
  <p>I have boiled it down to the most simple example I can below.  Any help you can provide would be appreciated:</p>
  
  <p>Inside of my UIView subclass's .h file:</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  @protocol TestDelegate &lt;NSObject&gt;
  @optional
  -(double)GetLineWidth;
  @end
  
  @interface ViewSubclass : UIView {
      id&lt;TestDelegate&gt; delegate;
  }
  
  @property (nonatomic, retain) id&lt;TestDelegate&gt; delegate;
  
  @end
  </code></pre>
  
  <p>Inside my delegate class's .h file:</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  #import "ViewSubclass.h"
  
  @interface ViewDelegate : NSObject &lt;TestDelegate&gt; {
  
  }
  
  @end
  </code></pre>
  
  <p>Inside my delegate class's .m file:</p>
  
  <pre><code>#import "ViewDelegate.h"
  
  @implementation ViewDelegate
  
  -(double)GetLineWidth {
      return 25.0;
  }
  
  @end
  </code></pre>
  
  <p>Inside my UIView subclass's .m file:</p>
  
  <pre><code>- (void)drawRect:(CGRect)rect
  {
      CGContextRef context = UIGraphicsGetCurrentContext();
  
      double lineWidth = 2.0;
  
      if (delegate == nil) {
          ViewDelegate *vd = [[ViewDelegate alloc]init];
          delegate = vd;
      }
  
      // If I comment out the "if" statement and just call the delegate
      // delegate directly, the call works!
      if ([delegate respondsToSelector:@selector(GetLineWidth:)]) {
          lineWidth = [delegate GetLineWidth];
      }
  
      CGContextSetLineWidth(context, lineWidth);
  </code></pre>