views:

794

answers:

3

I have a NSTextField and I want to set its content if I klick on a button and than set the cursor on this textfield at the end of the text so if someone klicks the button he could just begin to type.

Until now I use [NSTextField selectText] it selects this textfield but it selects the whole text so if someone just begins to type he'd lose all the text which alread is in the textfield.

A: 

You'll want to do something like this:

[[self window] makeFirstResponder:[self yourTextField]];

macinjosh
This also selects the text :-/
Jeena
+1  A: 

Ok I found it out now :)

- (IBAction)focusInputField:(id)sender {
    [textField selectText:self];
    [[textField currentEditor] setSelectedRange:NSMakeRange([[textField stringValue] length], 0)]; 
}

and in RubyCocoa it is:

def select_input_field
    @input.selectText self
    range = OSX::NSRange.new(@input.stringValue.length, 0)
    @input.currentEditor.setSelectedRange range
end
Jeena
A: 

I am writing an ingame plugini, I were having a similar problem:

- (void)gotEvent:(NSEvent *)newEvent
{
  [mainWindow makeKeyAndOrderFront:self];
  [mainWindow makeFirstResponder:messageField];
  [mainWindow sendEvent:newEvent]; // will forward the typing event
}

this does the job :)

Antwan van Houdt