1
votes

I have a NSTextField to get a path. The user can type a path or drag a folder in the text field. (I subclassed NSTextField to enable drag and drop). I want to do something each time the text is edited. I tried to link an action in IB and I implemented the delegate method from NSTextFieldDelegate

-(void)controlTextDidChange:(NSNotification *)obj  

But I only get a notification when the user change the text, not when it is changed programmatically.

Finally, I tried to redefine setStringValue

-(void)setStringValue:(NSString *)aString{
    [self textDidChange:[NSNotification notificationWithName:NSControlTextDidChangeNotification object:nil]];
    [super setStringValue:aString];
}

but I get an error

Ignoring exception raised in void run_cocoa_block(void *): *** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]

I guess it is because I should put something in object: but I have no idea what

1
Looks like you have to include an object (the new string) for the notification for some reason.jscs
I found a workaround by sending a notification in my subclass of NSTextField setstringvalue: method using: – addObserver:selector:name:object: – postNotificationName:object:impact27

1 Answers

0
votes

The object from NSNotification must not be nil. Put a reference to your text field instead.

[NSNotification notificationWithName:NSControlTextDidChangeNotification object:self];