Just to make sure that we are on the same page :)
ClassA has a delegate
ClassADelegate. What this means is that when some "event" occurs in
ClassA will want to notify some other class via its delegate that the "event" occurred -
ClassA will do this via its delegate -
For this to happen,
ClassB will have to let
ClassA know that it will be acting as
ClassB will have to "conform" to
ClassA's protocol by implementing all of the methods listed in the protocol that not marked as @optional.
In code, you could do this:
// ClassA's delegate
@protocol ClassADelegate <NSObject>
- (void) didDoSomethingCool:(NSString *) name;
// ClassA definition
// We'll use this property to call the delegate.
// id<XXX> means that which ever class is assigned to id MUST conform to XXX
@property (nonatomic, assign) id<ClassADelegate> classADelegate;
- (void) doSomething;
// Class A implementation
- (void) doSomething
// Do cool things here.
// Now call delegate, in this example, this will be ClassB
[classADelegate didDoSomethingCool:@"Hello from Class A"];
Now we need to wire-up
ClassB so that it can be notified that something happened in
// ClassB definition
// ClassB<ClassADelegate> lets the compiler know that ClassB is required to have all the
// non-optional method that are listed in ClassADelegate. In short, we say that
// ClassB conforms to the ClassADelegate.
Now somewhere in
ClassB's implementation file we have the following.
// ClassB implementation
- (id) init
self = [super init];
// Just quickly creating an instance of ClassA.
_classA = [ClassA new];
// This is were we tell ClassA that ClassB is its delegate.
_classA.classADelegate = self;
- (void) dealloc
- (void) didDoSomethingCool:(NSString *) name
// This is the method that ClassA will be calling via the
// [classADelegate didDoSomethingCool:@"Hello from Class A"] method call.
I hope this helps :)