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

时间:2011-05-04 14:26:55

标签: 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

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

1 个答案:

答案 0 :(得分:5)

respondsToSelector的{​​{3}}声明如下:

  

您无法测试是否有对象   从其超类继承一个方法   通过发送respondsToSelector:到   使用super关键字的对象。 [..]   因此,发送respondsToSelector:   超级相当于发送它   自我。相反,你必须调用   NSObject类方法   instancesRespondToSelector:直接   在对象的超类

这似乎可能是您的递归问题的原因。我不知道代表的东西是否相关。只是一个猜测。