在函数objective-c中使用协议中的静态函数
我想在函数中使用协议中的静态函数:在函数objective-c中使用协议中的静态函数,objective-c,ios,function,inheritance,protocols,Objective C,Ios,Function,Inheritance,Protocols,我想在函数中使用协议中的静态函数: @implementation IPadPanoramaViewController - (void)viewDidLoad { [self.view addSubview:[PanoramaContent getPanoramaContentByPanoramaItem:[[PanoramaListItem alloc] init]]; [super viewDidLoad]; } @end @
@implementation IPadPanoramaViewController
- (void)viewDidLoad
{
[self.view addSubview:[PanoramaContent getPanoramaContentByPanoramaItem:[[PanoramaListItem alloc] init]];
[super viewDidLoad];
}
@end
@protocol PanoramaItemProtocol
+ (UIView *) getPanoramaItemBySection;
@end
@implementation PanoramaContent
+ (UIView *) getPanoramaContentByPanoramaItem:(id<PanoramaItemProtocol>) itemKind {
return [itemKind getPanoramaItemBySection]; //here is the problem "unrecognized selector sent to instance"
}
@end
@implementation IPadPanoramaViewController
-(无效)viewDidLoad
{
[self.view addSubview:[PanoramContent GetPanoramContentByPanoramItem:[[PanoramListItem alloc]init]];
[超级视图下载];
}
@结束
@协议全景协议
+(UIView*)获取全景图项目分段;
@结束
@实现全景内容
+(UIView*)GetPanoramContentByPanoramItem:(id)itemKind{
return[itemKind getPanoramaItemBySection];//这是“无法识别的选择器发送到实例”的问题
}
@结束
我希望“全景列表项目”不要成为NSObject问题是,当您在实例上调用它时,您已经将
getPanoramaBySection
定义为类方法。在协议声明中,首先将+
替换为-,静态方法只能发送到类。如果不想创建对象,则GetPanoramContentByPanoramItem:应为Class类型。您可以使用以下内容:
+ (UIView *) getPanoramaContentByPanoramaItem:(Class)itemKind {
UIView *v = nil;
if( [itemKind respondsToSelector:@selector(getPanoramaItemBySection)] ) {
v = [itemKind getPanoramaItemBySection];
}
return v;
}
并发送信息:
[self.view addSubview:[PanoramaContent getPanoramaContentByPanoramaItem:[PanoramaListItem class]]];
什么是“全景列表项”?在示例代码中不使用它。