我最近尝试子类化 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/