选择器发送给类别中被覆盖的方法失败 [英] Selector Sent to method overridden in Category fails
问题描述
我正在尝试使用类别覆盖UIStoryboard方法。这是我的实现:
I am trying to override a UIStoryboard method using a category. Here is my implementation:
#import "UIStoryboard+SomeCategory.h"
#import <UIKit/UIKit.h>
@implementation UIStoryboard(SomeCategory)
-(id)instantiateInitialViewController
{
NSLog(@"SUPER CLASS: %@", [super class]); // logs "UIStoryboard"
NSLog(@"SUPER RTS : %@", [super respondsToSelector:@selector(instantiateInitialViewController)] ? @"YES" : @"NO"); // logs "YES"
return [super instantiateInitialViewController];
}
@end
当我添加时:
UIViewController *viewController = [super instantiateInitialViewController]
为什么会出现编译器错误:
Why do I get the compiler error:
Receiver type 'NSObject' for instance message does not declare a method with selector 'instantiateViewController'
推荐答案
请注意, [超类]
与 [自身超类]
不同。引用文档:
You should note that [super class]
is not the same as [self superclass]
. Quoting the docs:
Objective-C提供了两个术语,可在方法定义中使用它们来引用执行该方法的对象,即self和super。
它们在编译器搜索方法实现的方式上有所不同,在某些情况下,它们的含义相同。
They differ in how the compiler will search for the method implementation, and in some cases they will mean just the same.
在这种情况下,您需要:
In this case you want:
NSLog(@"SUPER CLASS: %@", [self superclass]); // logs "NSObject"
检查对象的超类,您将需要一个UIStoryBoard子类,而不是类别,可以使用:
to check an object's super class class, and you'll need a UIStoryBoard subclass, not a category, to be able to use:
return [super instantiateInitialViewController];
为什么 [超类]
不记录您期望的另一主题。如果您有兴趣,则此帖子什么是是Objective-C中的元类?是一个很好的起点。
Why [super class]
doesn't log what you expect is another subject. If you're interested, this post What is a meta-class in Objective-C? is a good starting point.
这篇关于选择器发送给类别中被覆盖的方法失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!