Question

I just implemented following method that suppose to take some action after the value of a NSTextField is changed in my NSOutlineView

-(BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor
{
    NSLog(@"end editing");
    NSTextField* tf = (NSTextField*)control;

    if(selectedItem && [selectedItem isKindOfClass:[HSCategoryClass class]])
    {
        HSCategoryClass* c = selectedItem;
        c.name = tf.stringValue;

        // request the update from DB
        [[NSNotificationCenter defaultCenter] postNotificationName:@"updatingCategoryName" 
                                                            object:c 
                                              userInfo:@{@"sender":self}];
    }
    return YES;
 }

However, when I'm done editing and hit enter key or navigate anywhere outside of the text field this method is getting called twice instead of just once.

Does anyone know why is this?!

Any kind of help is highly appreciated!

Was it helpful?

Solution

That routine does not signify that editing has ended. Instead, it's called to find out if it should end (hence the name of the method). It can be called by the framework any number of times, and you shouldn't be relying on it for this purpose.

Instead override the NSOutlineView's textDidEndEditing: method. Be sure to call super.

So you'd subclass the NSOutlineView and in your subclass:

- (void)textDidEndEditing:(NSNotification *)aNotification
{
  // do your stuff

  [super textDidEndEditing:aNotification];

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top