Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

iphone - Warning while adding and using a new method in external library protocol

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

LibraryController *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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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];
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...