title: Objective-C -- Subclass of delegate in subclass
body: <p>This is a fairly complicated inheritance hierarchy, so bear with me (I've tried to simplify things rather than state the exact case I am using which is even more complex):-</p>
  
  <p>Let's say I create a subclass of <code>UITextField</code> called <code>TextField</code> which is my own custom enhanced general-purpose textfield. Now, in order to provide this enhanced functionality, in the <code>init</code> method of <code>TextField</code>, I set <code>super.delegate = self</code> so that all the delegate methods from <code>UITextField</code> are sent to <code>TextField</code>. <code>TextField</code> implements the <code>UITextFieldDelegate</code> protocol and receives those delegate methods to do something interesting.</p>
  
  <p>However, in turn, I want to make it so that <code>TextField</code> has it's <em>own</em> delegate. So I create a new protocol called <code>TextFieldDelegate</code> (note the lack of <code>UI</code>-prefix!) and give <code>TextField</code> an ivar <code>id&lt;TextFieldDelegate&gt; __weak delegate</code> with corresponding property so that other classes can receive delegate methods from <code>TextField</code>.</p>
  
  <p>I hope you're still with me, because I haven't done anything too complex so far. But let's say that now, I create another custom subclass of <code>TextField</code>, let's call it <code>PasswordTextField</code> (in real life, one probably wouldn't need to create a subclass just to implement a password functionality, but let's assume that there is some fairly sophisticated implementation that would require this).</p>
  
  <p>Let's also assume that I want to make it so that <code>PasswordTextField</code> (which like <code>TextField</code> has a delegate property) is able to send an enhanced set of delegate methods. For example, maybe it can send a method <code>passwordIsSecure</code> which is sent once a password has reached a required level of complexity. Now since this behaviour that wouldn't be found in the regular <code>TextField</code>, I create a new protocol: <code>PasswordTextFieldDelegate &lt;TextFieldDelegate&gt;</code> which defines the new delegate methods for <code>PasswordTextField</code> <em>and</em> inherits all of the delegate methods sent by <code>TextField</code>.</p>
  
  <p>The problem is: how do I do implement this in <code>PasswordTextField</code>? Things that don't work:</p>
  
  <p><strong>Inheritance</strong></p>
  
  <p>I cannot simply inherit the delegate from <code>TextField</code>, because <code>TextField</code>'s delegate conforms only to <code>TextFieldDelegate</code> and not <code>PasswordTextFieldDelegate</code>, so I can't send methods like <code>[delegate passwordIsSecure]</code> because <code>TextFieldDelegate</code> has no such method.</p>
  
  <p><strong>Overriding ivar</strong></p>
  
  <p>I could try declaring an ivar in <code>PasswordTextField</code> called delegate, but the compiler complains that this is a duplicate declaration, because of course there is already an ivar called delegate in the superclass, so this doesn't work either*.</p>
  
  <p><strong>Modifying the superclass</strong></p>
  
  <p>I could go back to the <code>TextField</code> class and redefine the delegate to implement both <code>TextFieldDelegate</code> <strong>and</strong> <code>PasswordTextFieldDelegate</code>, but this seems messy and tells <code>TextField</code> that it can send <code>PasswordTextFieldDelegate</code> methods, which of course, it can't!</p>
  
  <p>I haven't tried this one, simply because it seems to break every sensible coding rule in the book.</p>
  
  <p>In summary, there must be some way of doing this such that a subclass of a class can have it's own delegate that's a sub-delegate of the superclass's delegate and for all of this to fit together nicely, but I just can't figure it out! Any ideas?</p>
  
  <p>(* As a side issue, I don't understand why the compiler complains when <code>PasswordTextField</code> declares a "duplicate" ivar named delegate, but doesn't complain when <code>TextField</code> declares an ivar named delegate which is presumably a duplicate of <code>UITextField</code>'s property called delegate!)</p>