当存在委托时,respondsToSelector如何表现?

Meg*_*aur 2 iphone delegates uitextfield

我最近尝试将UITextField子类化并将委托设置为我自己(发现这个尝试解决了我的问题:http://www.cocoabuilder.com/archive/cocoa/241465-iphone-why-can-a-uitextfield-be-its -own-delegate.html)

@interface MyObject :UITextField <UITextFieldDelegate>
@end

@implementation MyObject

-(id) initWithFrame:(CGRect) frame
{
  if((self=[super initWithFrame:frame]))
  {
      self.delegate=self;
  }
  return self;
}

-(BOOL) respondsToSelector:(SEL)selector
{
    NSLog(@"responds to selector");
    return [super respondsToSelector:selector];
}

// Implement all the missing methods
@end
Run Code Online (Sandbox Code Playgroud)

调用接口上定义的方法会导致无限递归.我在Apple文档中没有看到任何定义在一个委托在场的情况下responsesToSelector应该如何表现的内容.

Mar*_*man 5

文档respondsToSelector状态如下:

您无法通过使用super关键字向对象发送respondsToSelector:来测试对象是否从其超类继承方法.[..] 因此,发送respondsToSelector:to super相当于将其发送给自己.相反,您必须直接在对象的超类上调用NSObject类方法instancesRespondToSelector:

看来这可能是导致递归问题的原因.我不知道代表的东西是否相关.只是一个猜测.