Delegate problem with NSTextField
I am just starting with OSX development, and I am trying to get delegate notifications from a NSTextField. I've got the following code so far:
This is where I set the delegate:
- (void) awakeFromNib {
NSLog(@"Setting delegate");
[amountField setDelegate: [[TextfieldController alloc] initWithLog]];
}
And this is my TextfieldController:
- (TextfieldController *) initWithLog {
self = [super init];
NSLog(@"TextfieldController in开发者_StackOverflow社区itialized");
return self;
}
- (void)textDidChange:(NSNotification *)aNotification {
NSLog(@"textdidChange");
}
- (void)keyUp:(NSNotification *)aNotification {
NSLog(@"keyUp");
}
However, neither textDidChange nor keyUp is ever called... Not sure what I'm missing here, because the same way works just fine when I use it for my main window with the windowDidMiniaturize notification...
Anyone able to help? :)
The delegate should implement -controlTextDidChange:
, not -textDidChange:
. The -textDidChange:
method is a method on NSTextField which causes it to post a notification and call [self.delegate controlTextDidChange:]
. As for -keyUp:
, that's part of NSControl
's event-handling behaviour. If you want your delegate to receive a message when the textfield handles a key up event, you'll need to arrange that yourself by subclassing NSTextField
.
精彩评论