Given :
- classA with its delegate
- classB has a button on it
classA.h
@protocol classADelegate <NSObject>
- (void)method:(NSString *) name;
@end
@interface StoreChooser : UIViewController
@end
------------------------------------------------------
classA.m
-(IBAction)buttonCliked{
// invoke delegate method from classA at here
}
classB.h
@interface classB : UIViewController <classADelegate>
@end
------------------------------------------------------
// Conform delegate of classA
classB.m
- (void)method:(NSString *) name {
}
@end
——————————————————
My goal : I need classB to invoke a method delegate from classA in buttonClicked action
Question : what should I do to achieve my goal.
Just to make sure that we are on the same page 🙂
If
ClassAhas a delegateClassADelegate. What this means is that when some “event” occurs inClassA,ClassAwill want to notify some other class via its delegate that the “event” occurred –ClassB.ClassAwill do this via its delegate –ClassADelegate.For this to happen,
ClassBwill have to letClassAknow that it will be acting asClassA‘s delegate.ClassBwill have to “conform” toClassA‘s protocol by implementing all of the methods listed in the protocol that not marked as @optional.In code, you could do this:
Now we need to wire-up
ClassBso that it can be notified that something happened inClassA:Now somewhere in
ClassB‘s implementation file we have the following.I hope this helps 🙂