Re: [iPhone] Why can't a UITextField be its own delegate?
Re: [iPhone] Why can't a UITextField be its own delegate?
- Subject: Re: [iPhone] Why can't a UITextField be its own delegate?
- From: WT <email@hidden>
- Date: Sun, 26 Jul 2009 00:36:34 +0200
On Jul 26, 2009, at 12:23 AM, Kyle Sluder wrote:
On Sat, Jul 25, 2009 at 3:00 PM, WT<email@hidden> wrote:
What if the delegates are already subclassed? Since multiple
inheritance is
not an option, the code that limits the number of characters would
have to
be repeated in several places.
You always have categories, but that's not necessarily a good idea.
True, but you can't add instance variables to categories and the code
for limiting the number of characters (or a more general problem)
needs some ivars.
If you find yourself in this situation (class C needs behavior from
both class B and class A), you can always collapse the class hierarchy
and turn A and B into class AB, with options to configure its
behavior. That's how we wind up with things like NSController on the
desktop side, which can exist as an object controller or an entity
controller.
Now I'm the one who's going to say "that's convoluted". :)
This custom text field is still a text field in every way.
Then it should remain a UITextField.
There is no reason for any outside entity to want to refer to
super.delegate. In fact, unless I'm wrong, I don't think it's even
possible
to retrieve super.delegate from the custom instance.
Ooh, I think we've found your bug.
Internally, UITextField is going to use self.delegate to get its
delegate, following the correct accessor behavior. You've gone and
replaced -delegate to return self.
No. -delegate returns the "outside" delegate, not self. As I said,
from the outside world, everything is business as usual. If you call -
setDelegate: to set yourself up as the delegate, then you are what's
returned by a call to -delegate.
I might be mistaken but I think even internally UITextField would see
the outside delegate when it calls self.delegate. "self" tells the
runtime system to start the search at the bottom, which is
CustomTextField, not UITextField.
But the delegate pattern says that
messages which this object does not understand should be forwarded to
the delegate. This means that a class with a delegate needs to
implement -respondsToSelector: this way:
- (BOOL)respondsToSelector:(SEL)aSelector {
return [super respondsToSelector:aSelector]
|| [self.delegate respondsToSelector:aSelector];
}
Since -delegate is going to return self, -respondsToSelector: is going
to result in infinite recursion.
So It's Never Safe To Make An Object Its Own Delegate.
You might be on to something here... I have to think about the above.
Wagner
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden