iosobjective-c-protocol

Protocol does not responds to selector


I have a problem with Objective-C protocols.

I have defined protocol:

@protocol PlayerProfileSectionProReviewDelegate <NSObject>

- (void)didReceivedPlayerProfileSectionProReviewData;

@end

@interface PlayerProfileSectionProReviewModel : PlayerProfileSectionModel

@property (weak) id <PlayerProfileSectionProReviewDelegate> playerProfileSectionProReviewDelegate;

@end

In this class implementation I call delegate:

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

In view controller I have added PlayerProfileSectionProReviewDelegate and overriden didReceivedPlayerProfileSectionProReviewData method:

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

@end

and

#pragma mark <PlayerProfileSectionProReviewDelegate>

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

Why my protocol does not respond to selector?


Solution

  • Somewhere in your PlayerProfileSectionProReviewViewController class implementation, you need to set the appropriate PlayerProfileSectionProReviewModel object's delegate, like this:

    myModel.playerProfileSectionProReviewDelegate = self;
    

    If you do this, then when myModel reaches your delegate call, your view controller will receive it.

    By the way, you can simplify these lines:

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

    With:

    [self.playerProfileSectionProReviewDelegate didReceivedPlayerProfileSectionProReviewData];
    

    At this point if the delegate is nil, no message will be sent and you won't get any runtime error.