I'm trying to implement delegation. In the .h file of a custom class , I do this
#import <UIKit/UIKit.h>
@class Timer;
@protocol TimerDelegate
-(void)myClassDelegateMethod:(Timer *)timer;
@end
typedef void(^MyCustomBlock)(void);
@interface Timer : UILabel
@property (nonatomic, weak) id <TimerDelegate> delegate;
In the .m file I synthesize the delegate and also called the delegate method, checking to see first if the delegate implements the method
@synthesize delegate;
-(void)countdownTime:(NSTimer *)timer
{
NSLog(@"countdownTime called");
....
[self.delegate myClassDelegateMethod:self];
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(myClassDelegateMethod:)]) {
[self.delegate performSelector:@selector(myClassDelegateMethod:)];
} else {
NSLog(@"Delegate doesn't implement myClassDelegateMethod");
}
when I run my code, I'm told the delegate doesn't implement the method. Here's how I implement it In the viewController, I declare that it conforms to the protocol
@interface scViewController : UIViewController <TimerDelegate>
And then in the .m file of the viewController, I implement the delegate's method
- (void) myClassDelegateMethod:(Timer *) sender {
NSLog(@"Delegates are great!");
}
Can you explain how I've failed to implement the delegate method properly?
Update, in the viewController, I have a method that creates timer instances
-(Timer *)timer
{
_timer = [[Timer alloc] init];
return _timer;
}
In viewDidLoad, I do this
self.timer.delegate = self;