Geek@Paris

Stop editing NSTextField

2010-10-13

I only post some results here. Reference from http://www.cocoabuilder.com/archive/cocoa/230825-stop-edit-session-with-nstextfield.html?q=+Stop+edit+session+with+a+NSTextField#230825

Failed solutions:
1,

[[[self view] window] endEditingFor:nil];

Pressing return or pressing the tab key will not end the editing session
2,

[[[self view] window] makeFirstResponder:[self view]];

Pressing return will allow the editing session to end, but pressing the tab key will not
3,

NSTextView *fieldEditor = [[aNotification userInfo] objectForKey:@"NSFieldEditor"];
NSView *v = fieldEditor;
while ( v && ( [v superview] != [aNotification object] ) )
v = [v superview];
[v removeFromSuperview];

Pressing return or pressing the tab key will not end the editing session

The editing session was started with:

[textField selectText:nil];
NSTextView    *currentEditor    = (NSTextView*)[textField currentEditor];
NSPoint        windowLocation    = [theEvent locationInWindow];
NSPoint        screenLocation    = [[self window] convertBaseToScreen:windowLocation];
NSUInteger    characterIndex    = [currentEditor characterIndexForPoint:screenLocation];
[currentEditor setSelectedRange:NSMakeRange( characterIndex + 1, 0 )];

For me, I use [[[self view] window] makeFirstResponder:[self view]] to stop the text field by enter key.

And use NSWindow’s setAutorecalculatesKeyViewLoop:YES to stop the text field by tab key.

So both of them are solved.

Author:admin | Categories:Dev | Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *


Clickcha - The One-Click Captcha