• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: [iPhone] Why can't a UITextField be its own delegate?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [iPhone] Why can't a UITextField be its own delegate?


  • Subject: Re: [iPhone] Why can't a UITextField be its own delegate?
  • From: Kyle Sluder <email@hidden>
  • Date: Sat, 25 Jul 2009 15:23:21 -0700

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.
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.

> 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.  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.

--Kyle Sluder
_______________________________________________

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

  • Follow-Ups:
    • Re: [iPhone] Why can't a UITextField be its own delegate?
      • From: WT <email@hidden>
    • Re: [iPhone] Why can't a UITextField be its own delegate?
      • From: "Adam R. Maxwell" <email@hidden>
    • Re: [iPhone] Why can't a UITextField be its own delegate?
      • From: WT <email@hidden>
References: 
 >[iPhone] Why can't a UITextField be its own delegate? (From: WT <email@hidden>)
 >Re: [iPhone] Why can't a UITextField be its own delegate? (From: Brian Slick <email@hidden>)
 >Re: [iPhone] Why can't a UITextField be its own delegate? (From: WT <email@hidden>)
 >Re: [iPhone] Why can't a UITextField be its own delegate? (From: Luke the Hiesterman <email@hidden>)
 >Re: [iPhone] Why can't a UITextField be its own delegate? (From: WT <email@hidden>)
 >Re: [iPhone] Why can't a UITextField be its own delegate? (From: Kyle Sluder <email@hidden>)
 >Re: [iPhone] Why can't a UITextField be its own delegate? (From: WT <email@hidden>)

  • Prev by Date: Re: Multiple delegates Window
  • Next by Date: Re: [iPhone] Why can't a UITextField be its own delegate?
  • Previous by thread: Re: [iPhone] Why can't a UITextField be its own delegate?
  • Next by thread: Re: [iPhone] Why can't a UITextField be its own delegate?
  • Index(es):
    • Date
    • Thread