Ios 协议不响应选择器
我对Objective-C协议有问题 我定义了协议:Ios 协议不响应选择器,ios,objective-c-protocol,Ios,Objective C Protocol,我对Objective-C协议有问题 我定义了协议: @protocol PlayerProfileSectionProReviewDelegate <NSObject> - (void)didReceivedPlayerProfileSectionProReviewData; @end @interface PlayerProfileSectionProReviewModel : PlayerProfileSectionModel @property (weak) id &
@protocol PlayerProfileSectionProReviewDelegate <NSObject>
- (void)didReceivedPlayerProfileSectionProReviewData;
@end
@interface PlayerProfileSectionProReviewModel : PlayerProfileSectionModel
@property (weak) id <PlayerProfileSectionProReviewDelegate> playerProfileSectionProReviewDelegate;
@end
在视图控制器中,我添加了PlayerProfileSectionProReviewDelegate
并重写了didReceivedPlayerProfileSectionProReviewData
方法:
@interface PlayerProfileSectionProReviewViewController : PlayerProfileSectionViewController <UITableViewDelegate, UITableViewDataSource, PlayerProfileSectionProReviewDelegate>
@end
@interface PlayerProfileSectionProReviewViewController:PlayerProfileSectionViewController
@结束
及
#pragma标记
-(无效)未收到LayerProfileSectionProReviewData
{
[self.playerProReviewTableView重新加载数据];
}
为什么我的协议不响应选择器?在
PlayerProfileSectionProReviewViewController
类实现中的某个地方,需要设置相应的PlayerProfileSectionProReviewModel
对象的委托,如下所示:
myModel.playerProfileSectionProReviewDelegate = self;
如果执行此操作,则当myModel
到达代理调用时,视图控制器将收到该调用
顺便说一下,您可以简化这些行:
if ([self.playerProfileSectionProReviewDelegate respondsToSelector:@selector(didReceivedPlayerProfileSectionProReviewData)])
{
[self.playerProfileSectionProReviewDelegate didReceivedPlayerProfileSectionProReviewData];
}
与:
此时,如果委托为
nil
,则不会发送任何消息,也不会出现任何运行时错误 是否\u播放机配置文件部分proreviewdelegate
无
?另外,您可能希望将playerProfileSectionProReviewDelegate
定义为weak
属性。是的,\u playerProfileSectionProReviewDelegate
为nil
!如何处理?您已经定义了一个playerProfileSectionProReviewDelegate
属性,但这只是一个属性。这就像一个变量的名字——鞋盒。你没有给它任何价值。除非你让某个对象成为这个委托,否则那里没有人——鞋盒是空的(零)。。。但是,如何初始化它呢<代码>\u playerProfileSectionProReviewDelegate=[[playerProfileSectionProReviewDelegate alloc]init]代码>?你会看到的,非常感谢!这是一个愚蠢的错误,花了我这么长时间!
myModel.playerProfileSectionProReviewDelegate = self;
if ([self.playerProfileSectionProReviewDelegate respondsToSelector:@selector(didReceivedPlayerProfileSectionProReviewData)])
{
[self.playerProfileSectionProReviewDelegate didReceivedPlayerProfileSectionProReviewData];
}
[self.playerProfileSectionProReviewDelegate didReceivedPlayerProfileSectionProReviewData];