• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Injecting an NSResponder into the responder chain
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Injecting an NSResponder into the responder chain


  • Subject: Re: Injecting an NSResponder into the responder chain
  • From: Fritz Anderson <email@hidden>
  • Date: Fri, 30 Apr 2004 16:41:35 -0500

On 30 Apr 2004, at 4:01 AM, Glen Low wrote:

I want to handle the changeFont: message from NSFontPanel, or a custom message set using setAction:.

Now I do not use any custom NSView objects (or those that are custom don't want to bother with handling changeFont:) in the hierarchies,

(Actually the message is from NSFontManager...)

This is a pity, as it would seem that the handling of changeFont: would be a view-specific behavior, and the most straightforward way to add a view-specific behavior is to subclass the view and implement changeFont: there.

which really leaves three alternatives for catching this message:

1. My NSDocument subclass.
2. The main NSWindow delegate.
3. A custom NSResponder injected into the responder chain.

I don't like #1, since that means my NSDocument subclass needs to know about selected properties, what an NSFont is, etc... a bit too much of a MVC violation. (although if #3 proves to be tricky, I will go with this.)

I don't like #2, since this would handle the message at a very general level, and the delegate might want to do other things, whereas I just wanted a single, small object that handles the message and not much else.

Conceptually I tend to think, sending an object x a "changeFont:" message really does mean "change object x's font", so I can't see this applying to the NSDocument subclass or the NSWindow delegate (which is sort of like the window itself).

#3 seems to be it, but...

A. The Cocoa docs talk about "injecting it into the responder chain" -- how do I do this, and successfully remove it from the chain when I'm done?

In the Xcode full-text document search, I entered "inject responder." The first document returned explains that you can insert an NSResponder using the methods setNextResponder: and nextResponder:. It's a linked list, so you insert above an NSView by setting the responder's next responder to the NSView's nextResponder, and the NSView's nextResponder to the new responder. The new responder would want to remember the view it was inserted above, for ease in removal. The article says you can't inject above NSWindow.

B. What happens after I inject it into the chain, and the user clicks on some other NSTextView and my responder is no longer in the chain?

Override NSWindow's makeFirstResponder:. If the inherited method returns YES, and the new responder is non-nil, pull the injected responder from its previous position and insert it into the new one. The window will have to keep a reference to the responder object.

Ugly. Which is why, knowing as little about your situation as I do, I'd prefer to subclass the views.

-- F

--
Fritz Anderson
Consulting Programmer
http://resume.manoverboard.org/
_______________________________________________
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.


References: 
 >Injecting an NSResponder into the responder chain (From: Glen Low <email@hidden>)

  • Prev by Date: Re: Three Views in a Row
  • Next by Date: Sliding an image
  • Previous by thread: Injecting an NSResponder into the responder chain
  • Next by thread: NSImage allocation issues
  • Index(es):
    • Date
    • Thread