Delegate events for NSTextField in NSOutlineView?

I have impeccable functionality based on viewing NSOutlineView with the appropriate source data source in my project. Now I want to allow the user to modify certain entries. So I made NSTextField in IB editable. For the NSOutlineView cell NSOutlineView you can use the outlineView:setObjectValue:forTableColumn:byItem: delegate outlineView:setObjectValue:forTableColumn:byItem: however it is not available for the NSOutlineView based on the view as specified in the header file for the NSOutlineViewData protocol:

/ * View Based OutlineView: This method is not applicable. * /

(void) outlineView: (NSOutlineView *) outlineView setObjectValue: (id) object forTableColumn: (NSTableColumn *) tableColumn byItem: (id) item;

So, I was looking for another delegate method and found outlineView:shouldEditTableColumn:item: However, this delegate method does not start. Perhaps because I am not editing the cell.

So my question is: is there any other way to notice when a line has changed than to have a delegate for each NSTextField ?

+4
source share
2 answers

Well, it looks like Apple wants us to use delegate methods for each NSTextField , as stated here :

This method is intended for use with table views in cells; it should not be used with view-based views. Instead, a goal / action is used for each element in the view cell.

So there is currently no other way to do this.

+4
source

You are correct that your text field should be editable in Interface Builder.

Then so that your controller matches NSTextFieldDelegate. Then set the delegate for the text field to outlineView: viewForTableColumn: item :, like so:

 tableCellView.textField.delegate = self 

Here's a simplified example where you have implemented a method to return a table cell view for an item for your plan.

 -(NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item { NSTableCellView *tableCellView = [outlineView makeViewWithIdentifier:@"myTableCellView" owner:self]; MyItem *myItem = (MyItem *)item; // MyItem is just a pretend custom model object tableCellView.delegate = self; tableCellView.textField.stringValue = [myItem title]; tableCellView.textField.delegate = self; return result; } 

Then the controller should receive a controlTextDidEndEditing notification:

 - (void)controlTextDidEndEditing:(NSNotification *)obj { NSTextField *textField = [obj object]; NSString *newTitle = [textField stringValue]; NSUInteger row = [self.sidebarOutlineView rowForView:textField]; MyItem *myItem = [self.sidebarOutlineView itemAtRow:row]; myItem.name = newTitle; } 
+9
source

Source: https://habr.com/ru/post/1388596/


All Articles