I have a subclass of NSTextField, which I made so that when the user has finished editing the field, the text field will lose focus. I also set it up so when the user clicks on the main view, this will act as a loss of focus on the text field. And all this works great. Now I want to add some additional features to the subclass.
I want the text field to send textDidEndEditing every time the user clicks anywhere outside the field. This includes when the user clicks on another component of the user interface. The behavior that I see now is that when a user clicks on another component of the user interface (say, a combo box), the action does not start. Is there any way to force this? Also, manually adding it as part of the actions of other components?
Any help would be appreciated!
Here is the code for the textDidEndEditing function
- (void)textDidEndEditing:(NSNotification *)notification
{
NSString *file = nil;
char c = ' ';
int index = 0;
[super textDidEndEditing:notification];
if ([self isEditable])
{
file = [self stringValue];
if ([file length] > 0)
{
c = [file characterAtIndex:([file length] - 1)];
if (c == '\n')
{
NSMutableString *newfile = [[NSMutableString alloc] init];
c = [file characterAtIndex:index++];
do
{
[newfile appendFormat:@"%c", c];
c = [file characterAtIndex:index++];
}
while ((c != '\n') && (index < [file length]));
[self setStringValue:newfile];
file = newfile;
}
[[NSNotificationCenter defaultCenter]
postNotificationName:@"inputFileEntered" object:self];
}
}
[self setSelectedText:0];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"setResponderToNil" object:self];
}
Where "setSelectedText" is a public function in a subclass of the text field:
- (void)setSelectedText:(int) length
{
int start = 0;
NSText *editor = [self.window fieldEditor:YES forObject:self];
NSRange range = {start, length};
[editor setSelectedRange:range];
}
And the setResponderToNil notification is part of my NSView subclass:
- (void)setResponderToNil
{
AppDelegate *delegate = (AppDelegate *)[NSApp delegate];
[delegate.window makeFirstResponder:nil];
}