Disabling text completion in an NSTextField

Q: How do you turn off text completion in a given NSTextField?

A: You can disable text completion in a text field by assigning a delegate that returns nil from the method control:textView:completions:forPartialWordRange:indexOfSelectedItem:.

Listing 1: Implementing the delegate method.

- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words
                    forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index {
    return nil;
}

To set the delegate for the text field either drag a connection from the text field in Interface Builder or send the field -setDelegate: when the nib file loads (likely in an -awakeFromNib or -windowControllerDidLoadNib method).

Note: To fully understand what the delegate of a text field can respond to, you'll want to look at the documentation for several classes in addition to NSTextField. Many general control behaviors and even text-specific behaviors are inherited from NSControl. While editing is underway in the text field, an instance of NSTextView is also acting as the fieldEditor, so the delegate methods of NSText are also of interest.

See the Cocoa documentation for more infomation on delegation, Text Fields, the NSControl class, and working with the field editor.

Document Revision History

DateNotes
2008-02-27First Version

Posted: 2008-02-27


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.