-1

我对 Objective-C 协议有疑问。

我已经定义了协议:

@protocol PlayerProfileSectionProReviewDelegate <NSObject>

- (void)didReceivedPlayerProfileSectionProReviewData;

@end

@interface PlayerProfileSectionProReviewModel : PlayerProfileSectionModel

@property (weak) id <PlayerProfileSectionProReviewDelegate> playerProfileSectionProReviewDelegate;

@end

在这个类实现中,我调用委托:

if ([self.playerProfileSectionProReviewDelegate respondsToSelector:@selector(didReceivedPlayerProfileSectionProReviewData)])
{
    [self.playerProfileSectionProReviewDelegate didReceivedPlayerProfileSectionProReviewData];
}

在视图控制器中,我添加PlayerProfileSectionProReviewDelegate并覆盖了didReceivedPlayerProfileSectionProReviewData方法:

@interface PlayerProfileSectionProReviewViewController : PlayerProfileSectionViewController <UITableViewDelegate, UITableViewDataSource, PlayerProfileSectionProReviewDelegate>

@end

#pragma mark <PlayerProfileSectionProReviewDelegate>

- (void)didReceivedPlayerProfileSectionProReviewData
{
    [self.playerProReviewTableView reloadData];
}

为什么我的协议不响应选择器?

4

1 回答 1

0

在您的PlayerProfileSectionProReviewViewController类实现中,您需要设置适当PlayerProfileSectionProReviewModel的对象的委托,如下所示:

myModel.playerProfileSectionProReviewDelegate = self;

如果你这样做,那么当myModel到达你的委托调用时,你的视图控制器将收到它。

顺便说一句,您可以简化这些行:

if ([self.playerProfileSectionProReviewDelegate respondsToSelector:@selector(didReceivedPlayerProfileSectionProReviewData)])
{
    [self.playerProfileSectionProReviewDelegate didReceivedPlayerProfileSectionProReviewData];
}

和:

[self.playerProfileSectionProReviewDelegate didReceivedPlayerProfileSectionProReviewData];

此时,如果委托是nil,则不会发送任何消息,您也不会收到任何运行时错误。

于 2015-01-29T06:59:22.063 回答