Re: Make NSTextField only send action on <Return>
Re: Make NSTextField only send action on <Return>
- Subject: Re: Make NSTextField only send action on <Return>
- From: Henry McGilton <email@hidden>
- Date: Fri, 5 Sep 2003 11:02:07 -0700
On Friday, September 5, 2003, at 09:50 AM, Jens Bauer wrote:
Hi all,
I have a NSTextField in my interface, and would like it to send an
action, but only if the Return key is pressed.
If the Tab key is pressed or if another text-field is clicked, no
action should be sent.
How do I do this properly ?
I've had a look at sendActionOn: but I didn't find a working solution
here.
Is it possible to find out from the target object 'why' the action
method was called ?
-Or do I have to subclass NSTextField to split the Return and
vier-change into 2 separate actions?
Take a look at the text delegate methods defined in NSControl.
Using a delegate, you can determine what text movement action
was used to end the editing, and then you can decide what to
do with the thing. Set your controller object to be a delegate of the
text field, and implement the controlTextDidEndEditing method.
Something like this:
- (void)controlTextDidEndEditing:(NSNotification *)aNotification
{
NSControl *textfield = [aNotification object]; // tells you the
text field sending the notification
NSDictionary *dict = [aNotification userInfo]; // get the user
info dictionary from the notification
NSNumber *reason = [dict objectForKey: @"NSTextMovement"]; //
get the text movement number from the dictionary
int code = [reason intValue]; //
get the text movement code
if (code == NSReturnTextMovement) { // deal with return
;
} else if (code == NSTabTextMovement) { // deal with tab
;
} // and so on and so on
}
The set delegate to text field is defined in NSTextField. The
delegate methods are described (in a very sketchy way) in NSControl,
even though NSControl does not actually have a delegate. The text
movement codes are defined in NSText, and also in the App Kit
Types and Constants under 'Enumerations'. And there is some
more sketchy information in the documentation for NSNotification.
I don't know if there is any actual description in one place of all
the things that you can get out of the NSNotification user info
dictionary --- I had to guess at this stuff and kind of flail
around until it worked --- not what I call engineering . . .
Best Wishes,
........ Henry
===============================+============================
Henry McGilton, Boulevardier | Trilithon Software
Objective-C/Java Composer | Seroia Research
-------------------------------+----------------------------
mailto:email@hidden |
http://www.trilithon.com
|
===============================+============================
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.