开发者

objective c Delegate on a existing category methods

I want my class to delegate on the same methods of NSObject (NSURLConnectionDelegate) category. But I always get " unrecognized selector" when trying to delegate. Here a simple code I use to test. Where did I go wrong?

@interface DelegateTest : NSObject
{
id intenalDelegate;
}

-(void) setDelegate: (id) delegate;
-(void) executeDelegateMethod;

@end


@implementation DelegateTest

-(void) setDelegate:(id) delegate{

intenalDelegate = [delegate retain];
}
-(void) executeDelegateMethod{
if([intenalDelegate      respondsToSelector:@selector(connection:canAuthenticateAgainstProtectionSpace:)]) {
    return  [intenalDelegate connection:nil canAuthenticateAgainstProtectionSpace:nil];}

}
@end

In the classe I re-implemented the method I am requesting, I wrote:

DelegateTest * test = [[DelegateTest alloc]init];
[test setDelegate:self];
[test开发者_JAVA百科 executeDelegateMethod];

but I got "unrecognized selector" error. Thanks indvance for any adevice.


InternalDelegate is of type id so the compiler has NO IDEA what methods are on that object. You normally declare delegates as follows...

@protocol MyDelegate<NSObject> {
    - (void)myMethod;
}

Then in the interface you declare the instance variable as...

id<MyDelegate> internalDelegate;

Remember though with properties to use the assign attribute as you don't own the delegate...

@property (nonatomic, assign) id<MyDelegate> internalDelegate;

Note: that making the delegate implement the NSObject protocol ensures you can call respondsToSelector on it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜