Re: Notification questions
Re: Notification questions
- Subject: Re: Notification questions
- From: Bill Cheeseman <email@hidden>
- Date: Tue, 14 Dec 2004 15:50:58 -0500
on 2004-12-14 1:02 PM, Mark Alldritt at email@hidden wrote:
> 1) I want to receive a notification whenever a window is made visible on
> screen (within my app). NSWindow does not seem to offer a notification
> message that gives this information.
This might depend on the circumstances under which, or how, it became
visible. If it's a preference window or a palette that is hidden and shown
when the application is deactivated or activated, you might be able to use
one of the NSApplication notifications,
NSApplicationDidBecomeActiveNotification and
NSApplicationDidResignActiveNotification, or their "Will" variants.
In NSWindow, you can experiment with ...DidBecomeKeyNotification and
...DidBecomeMainNotification, but they usually aren't suitable for something
like this. I gather that ...DidExposeNotification isn't what you want, since
you wouldn't have asked the question if it was.
There have long been complaints about "missing" notifications and delegate
methods in NSWindow around a window's coming to the front and so on. You
might be able to write a category on NSWindow to add what you want, if you
can figure out a way to code it.
> 2) I want to receive a notification whenever a text field becomes the first
> responder. Again, I don't see a notification that broadcasts this
If I recall correctly, a text field never becomes first responder. Instead,
the window's field editor becomes first responder, and its delegate is
switched to be the text field that is associated with the field editor when
focus changes. This is easy to forget, even when you know it already. It
complicates handling the sort of issue you're asking about in the specific
case of text fields.
I think this is why you can't subclass NSTextField and override
-[NSResponder becomeFirstResponder] to do what you're looking for.
Basically, you would like to override this method to post a custom
notification of your own. At page 411 of my book I suggested using
-becomeFirstResponder for this purpose but said that I couldn't make it
work. I think this was because a text field never becomes first responder.
There is probably some way you can take advantage of the field editor to do
what you want. For example, -windowWillReturnFieldEditor:toObject: is called
by -fieldEditor:forObject:, and if I recall correctly the latter is called
by the system whenever the user selects a text field. So you could try
overriding the former or the latter to post a custom notification. See page
412 of my book. (-fieldEditor:forObject: is called at other times, as well,
so you'll have to do some filtering.)
You could also check whether the field editor itself receives
-becomeFirstResponder when a new textfield is selected. If so, you might be
able to override it to accomplish your goal, after all. Come to think of it,
I suppose the field editor must receive this, but the question is whether it
receives it when the user selects one text field while another text field
has focus, as opposed to while some other kind of control has focus.
--
Bill Cheeseman - email@hidden
Quechee Software, Quechee, Vermont, USA
http://www.quecheesoftware.com
PreFab Software - http://www.prefab.com/scripting.html
The AppleScript Sourcebook - http://www.AppleScriptSourcebook.com
Vermont Recipes - http://www.stepwise.com/Articles/VermontRecipes
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden