Search code examples
swiftmacosnstableviewnstextfieldnsoutlineview

How to prevent right-click textfield renaming in NSOutlineView


I am having a NSOutlineView in which the textfield is editable by either pressing return or using the right click. I am also overriding the menu(for event: NSEvent) -> NSMenu? to menu based on which row is clicked

When I right click on the textfield, it opens the correct menu as expected but it also makes the textfield to go into edit mode. Is there a way to handle this behaviour?

Wrong

However, when I click outside the textfield then it works without setting the textfield in edit mode:

Right


Solution

  • I have a similar NSTableView where right-clicking is supported to display a menu, but the fields are also directly editable. I took a look to see why/how the editing doesn't seem to be a problem in my case, and I narrowed the behavior down to the presence of this line of code in my NSTableView subclass:

    // This trick convinces the accessibility system to bother checking whether
    // we have a menu to export to e.g. VoiceOver.
    [self setMenu:[[NSMenu alloc] init]];
    

    As you can see from the comment, the rationale for this "setting an empty menu" was for a different reason than avoiding the editing behavior you're seeing, but it appears to have the side-effect of also fixing that.

    So, please try adding a line like the above to your NSOutlineView subclass and see if it fixes the problem!