iphone - 当存在委托(delegate)时,respondsToSelector 的行为如何?

标签 iphone delegates uitextfield

我最近尝试子类化 UITextField 并将委托(delegate)设置为我自己(发现这试图解决我的问题: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

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

最佳答案

docs对于 respondsToSelector 声明如下:

You cannot test whether an object inherits a method from its superclass by sending respondsToSelector: to the object using the super keyword. [..] Therefore, sending respondsToSelector: to super is equivalent to sending it to self. Instead, you must invoke the NSObject class method instancesRespondToSelector: directly on the object’s superclass

看来这可能是导致您的递归问题的原因。我不知道委托(delegate)的事情是否相关。不过只是猜测。

关于iphone - 当存在委托(delegate)时,respondsToSelector 的行为如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5884985/

相关文章:

iphone - selectall uitextfield 并不总是全选

iphone - xCode 6 的新模拟器上的可调整大小的 iPhone 和可调整大小的 iPad 是什么?

ios - NSObject Delegate类,将Delegate传递给UIViewcontroller类

ios - 我的 subview Controller 的弱属性或强属性?

javascript - 模块化 JS 组件中的委托(delegate)模式

validation - iOS 7 中的 UITextField 输入验证

iphone - 检查 ASIFormDataRequest 是否仍在进行中或完成获取数据的最佳方法是什么?

iphone - NSPredicate 聚合函数 [SIZE] 给出 'unsupported function expression' 错误

ios - FetchRequest - NSArray 元素无法匹配 Swift 数组元素类型 - Swift 2.0

ios - 启用/禁用 UIButton 取决于 UITextField 中文本的长度