Edit AllPages

Cocoa allows you to substitue a custom FieldEditor, Apple provides documentation [] but the examples have a few bugs. See CreatingCustomFieldEditor for discussion, including broken code examples, on this topic, this page is an example of how it’s actually supposed to work.

Why use a custom field editor?

If you want to change the default behaviour of text fields being edited in your application, you want a custom FieldEditor. There are other cases discussed in CreatingCustomFieldEditor and the Apple documentation.

What’s the Setup?

You will add a field editor to the NSWindowController subclass, in this example it will be an NSTextField.

// MyWindowController.h

@interface MyWindowController : NSWindowController { NSTextField* custom_field_editor; }


What’s the implementation?

// MyWindowController.m

#import “MyWindowController.h”

@implementation MyWindowController

#pragma mark NSWindowDelegate Methods

(id) windowWillReturnFieldEditor:(NSWindow*) sender toObject:(id) anObject { if ([anObject isKindOfClass:[NSTextField class]]) { // do any per-edit setup here (e.g. select all text) return custom_field_editor; } return nil; }


Has anyone actually used this? It seems all kinds of messed up. Aside from the underscores in variable names, it’s declaring a pointer to an NSTextField, and then allocating an NSTextView. And there should be the actual custom field editor somewhere.

Yes, it works for me (David Phillip Oster). Except: my custom field editor is a subclass of NSTextView, and my windowWillReturnFieldEditor: always returns the field editor. I use it because my tableview’s controller sets the text of cut/copy/paste menu items to “Cut rows”, etc. so I needed a subclass of NSTextView so the field editor could set the text of the menu items back.