I am using a external library and one of my view controller is registering as delegate for a class in that framework. Now, at one place I want to execute some code on this delegate class. I am writing a method for that and calling it on my delegate.
Now, all works fine but I am getting a warning that this newly added method is not part of the protocol.
This is my Class:
@protocol MyExtendedDelegate <LibraryDelegate>
@optional
- (void)actionTaken;
@end
@interface MyController : UITableController <MyExtendedDelegate> {
}
@end
And inside my controller I am registering self as delegate for library controller
Li开发者_StackOverflow社区braryController *libController = [[LibraryController alloc] init];
libController.delegate = self;
Finally, This is the code in a separate class where I am calling this method:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([self.libraryController.delegate respondsToSelector:@selector(actionTaken)]) {
[self.libraryController.delegate actionTaken];
}
Here is the warning I am getting:
-- actionTaken not found in protocol -- NSObject may not respond to actionTaken
I want to get rid of this warning. Any idea.
The property libraryController.delegate
is defined in the external library to conform to LibraryDelegate
. Try to downcast to MyExtendedDelegate
before you call the method from your extended protocol.
if ([self.libraryController.delegate conformsToProtocol:@protocol(MyExtendedDelegate)])
{
id<MyExtendedDelegate> extendedDelegate = (id<MyExtendedDelegate>)self.libraryController.delegate;
if ([extendedDelegate respondsToSelector:@selector(actionTaken)])
{
[extendedDelegate actionTaken];
}
}
Write a new protocol that extends the old one, and conform to that, something like:
@protocol MyNewProtocol <OtherProtocol>
- (void) myCoolMethod;
@end
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if ([self.libraryController.delegate respondsToSelector:@selector(actionTaken)]) { [self.libraryController.delegate performSelector:@selector(actionTaken)]; }
Using performSelector instead of directly calling a method will remove warning for sure.
精彩评论