-1

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?

Jacob Jones
  • 501
  • 2
  • 6
  • 22
  • 1
    Is `_playerProfileSectionProReviewDelegate` `nil`? Also, you probably want to define `playerProfileSectionProReviewDelegate` as a `weak` property. – Aaron Brager Jan 28 '15 at 23:29
  • Yes, `_playerProfileSectionProReviewDelegate` is `nil`! How to deal with it? – Jacob Jones Jan 28 '15 at 23:32
  • You have defined a `playerProfileSectionProReviewDelegate` property, but that's just a property. It's like the name of a variable - a shoebox. You have not given it any _value_. Unless you make some object _be_ this delegate, there is nobody there - the shoebox is empty (nil). – matt Jan 29 '15 at 01:08
  • Hmmm... But then, how to initialise it? `_playerProfileSectionProReviewDelegate = [[PlayerProfileSectionProReviewDelegate alloc] init];` ? – Jacob Jones Jan 29 '15 at 04:47
  • You will see that http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c – Sergey Petruk Jan 29 '15 at 06:37

1 Answers1

0

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.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
Romain
  • 3,718
  • 3
  • 32
  • 48