I have a class which has it’s delegate:
@protocol SNIRCControllerDelegate
- (void) serverTalked:(id)data;
@end
@interface SNIRCController : NSObject <NSStreamDelegate> {
id<SNIRCControllerDelegate> delegate;
}
- (void) setDelegate:(id<SNIRCControllerDelegate>)_delegate;
- (void) test;
@end
The implementation:
@implementation SNIRCController
- (void) setDelegate:(id<SNIRCControllerDelegate>)_delegate {
_delegate = delegate;
}
- (void) test {
[delegate serverTalked:@"test"];
}
But for some reason [delegate serverTalked:@"test"]; doesn’t calls the delegate :/
This is how I do it on the AppDelegate:
@interface AppDelegate : NSObject <NSApplicationDelegate, NSStreamDelegate, SNIRCControllerDelegate> {
IBOutlet NSTextView *logField;
SNIRCController *ircController;
}
@property (assign) IBOutlet NSWindow *window;
-(void)writeToLog:(NSString*)data;
@end
@implementation AppDelegate
@synthesize window = _window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
ircController = [[SNIRCController alloc] init];
[ircController setDelegate:self];
[ircController test];
}
- (void) serverTalked:(id)data {
NSLog(@"got called :D");
}
-(void)writeToLog:(NSString*)data {
NSAttributedString *stringToAppend = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n", data]];
[[logField textStorage] appendAttributedString:stringToAppend];
}
But serverTalked: doesn’t gets called 🙁 what I’m doing wrong?
In your implementation of
setDelegate:, this:should be:
You got confused and swapped ivar and parameter. The confusion might have been caused by the fact that the underscore prefix is more commonly used for ivars. In your case, it is the parameter that is prefixed with an underscore.