If I have a delegate which resides within its own header file myDelegate.h. Then I have a class (ClassOne) that implements the delegate protocol (therefore implementing the delegate function(s)). Then I create another class (ClassTwo) that has a instance variable of myDelegate. Can I then use this variable to call the function that resides in ClassOne?
Here is the code:
//myDelegate.h
@protocol myDelegate <NSObject>
- (BOOL)myFunction:(NSString*)sString;
@end
//ClassOne.h
@interface ClassOne : NSObject <myDelegate> {
}
- (BOOL)myFunction:(NSString*)sString;
@end
//ClassOne.m
#import "ClassOne.h"
@implementation ClassOne
- (BOOL)myFunction:(NSString*)sString
{
//do stuff
}
@end
//ClassTwo.h
#import "myDelegate.h"
@interface ClassTwo : NSObject {
id<myDelegate> del;
}
@property (nonatomic, retain) id<myDelegate> del;
@end
//ClassTwo.m
#import "ClassTwo.h"
@implementation ClassTwo
- (void)aFunction:(NSString*)string
{
[del myFunction:string];
}
@end
Yes, that is exactly right.
Except
myDelegateshould beMyDelegate. It’s not a syntax error and will execute perfectly, but standard objective-c conventions say that you should never define a delegate with a lowercase first character.