I saw the example with Flower an MyGarden, but when I try to make something similar it doesn’t work. My code:
ClassA.h:
#import <Foundation/Foundation.h>
@protocol CommDelegate <NSObject>
@required
-(void)funcB;
@end
@interface ClassA : NSObject
{
id <CommDelegate> delegate;
}
@property (retain) id delegate;
-(void)funcB;
@end
ClassA.m
#import "ClassA.h"
@implementation ClassA
-(void) start
{
[[self delegate] funcB];
}
@end
ClassB.h
#import <Foundation/Foundation.h>
#import "ClassA.h"
@interface ClassB : NSObject <CommDelegate>
@end
ClassB.m
#import "ClassB.h"
@implementation ClassB
-(void)funcB
{
NSLog(@"HELLO!");
}
@end
And in main I’m doing:
ClassA* classa = [[ClassA alloc] init];
[classa start];
Could someone help me, and tell what I’m doing wrong?
Delegation overview summary:
delegator.delegate = self;
The delegate is just a pointer to an object that will implement the necessary methods for you. In your case, I think you want classA to be able to call a funcB method, but for that funcB to be implemented by someone else.
There needs to be a delegate property in the class that wants to have another class implement these methods and then in the class that is to be the delegate, it should set the delegate property to self.
As others have noted, you don’t set your delegate. Somewhere in classB you need a