- (void)notifyDelegateOfButtonTapThe protocol definition would typically look like this when implementing this pattern:
if (self.delegate &&
[self.delegate protocolSampleViewController:self didTapButton:self.button];
@protocol ProtocolSampleViewControllerDelegateThis is a typical protocol definition as code completed in Xcode 3.x, with the addition of @optional just to maintain pattern correctness for the example. In Xcode 4.x, the code completed @protocol definition template looks like this:
- (void)protocolSampleViewController:(ProtocolSampleViewController *)protocolSampleViewController didTapButton:(UIButton *)button; @end
@protocol protocol name <NSObject>As you might have guessed by now, the <NSObject> added behind the protocol name is the key. That's because it's actually the NSObject protocol that defines most of the core methods that the NSObject class implements, including respondsToSelector:
Once you make sure all your protocol definitions adopt the <NSObject> protocol, then calling respondsToSelector: on your delegate or datasource variables won't result in compiler warnings.
One more thing...you could cast your delegate or data source variable to id like this:
...That is an ugly solution and doesn't help you understand another key feature of protocols, inheritance, which I plan to cover in an upcoming post.