Question

Cocoa newbie here: and I seem to be getting into all the really arcane stuff first. I wanted an NSTextField in which the contents were selected by default when clicked, and in which I could change the selection color. I found enough on this forum to get me started in the mysteries of field editors. I have NSTextField subclassed, and the becomeFirstResponder method overridden:

- (BOOL) becomeFirstResponder {
    if (!super.becomeFirstResponder) return NO;
    NSTextView * fieldEditor = (NSTextView *) [self.window fieldEditor:YES forObject:self];
    [fieldEditor performSelector:@selector(selectAll:) withObject:fieldEditor afterDelay:0];
    [fieldEditor setSelectedTextAttributes:selectionColor];
    return YES; }

Where selectionColor is a one-item NSDictionary with an NSColor object keyed to NSBackgroundColorAttributeName. This works perfectly for text fields on windows and panels, but doesn’t work at all for fields in the view of an NSPopover. In fact, it makes text selection impossible. It is still possible to click in the field and type, but dragging the cursor through text to select it fails. I can only conclude I’m misconfiguring the field editor somehow. Am I referencing it correctly?

Was it helpful?

Solution

I hope I’m following stackOverflow protocol in answering my own question. The short answer is that, apparently, the code I posted works perfectly well for NSPopovers. The probable reason I thought it wasn’t working was that the NSDictionary object I called selectionColor wasn’t being correctly initialized. I’m having an Emily Litella moment here: “Oh… Never mind!” The only problem now, as I see it, is that it took me a ridiculous amount of time to track this one down.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top