There are 3 classes Red, Blue and Yellow. Whenever a button is pressed yellow class informs the blue class and passes back a string through delegate. This bit works fine. Well now the blue class is trying to call the red class instance method displayString inside the delegate implementation method. But this instance method is never called for some reason. Upon debugging i found that self.red is null in [self.red displayString:col];. But i use self.red in another class instance method of Blue class and self.red is NOT NULL then. Is something missing here
@interface RedClass : NSObject
-(void)displayString:(NSString*)str;
@end
@implementation RedClass
-(void)displayString:(NSString*)str
{
NSLog(@"The string:%@",str);
}
@end
// BlueClass.h
@interface BlueClass:UIViewController<YellowDelegate>
@property (nonatomic, strong) RedClass *red;
@end
//the class that is implementing the delegateExample and calling the class instance method of example class
// BlueClass.m
@implementation BlueClass
- (void)colorChanged:(NSString*)col
{
[self.red displayString:col];
}
@end
//the YellowClass
@protocol YellowDelegate<NSObject>
-(void)colorChanged:(NSString *)col;
@end
@interface YellowClass:UIView
@property (nonatomic, weak) id <YellowDelegate> delegate;
@end
//YellowClass.m @implementation YellowClass
(IBAction)btnPressed:(id)btn
{
[self.delegate colorChanged:@"yellow"];
}
@end