Delegate events for NSTextField in a view-based NSOutlineView?

左心房为你撑大大i 提交于 2019-12-04 11:25:33

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

Next, make your controller conform to NSTextFieldDelegate. Then, set the delegate for the text field in outlineView:viewForTableColumn:item:, like so:

tableCellView.textField.delegate = self

Here's a simplified example, where you've implemented the method for returning the table cell view for an item for your outline view.

-(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 get 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;  
}
Paul

Well, it seems like Apple wants us to use the delegate methods of each NSTextField as stated here:

This method is intended for use with cell-based table views, it must not be used with view-based table views. Instead target/action is used for each item in the view cell.

So there's currently no other way to do this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!