• 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: NSDocument Enabling and handling menu items without implementing the action method in the document class
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDocument Enabling and handling menu items without implementing the action method in the document class


  • Subject: Re: NSDocument Enabling and handling menu items without implementing the action method in the document class
  • From: Eyal Redler <email@hidden>
  • Date: Mon, 20 Jul 2009 05:03:56 +0300

Thank you very much Graham!
Looks just like what I was looking for.

Eyal


On Jul 19, 2009, at 3:46 PM, Graham Cox wrote:


On 19/07/2009, at 10:06 PM, Eyal Redler wrote:

This works fine but it requires me to write an annoying amount such glue code every time I add an action so I'm looking for a better way to do this. Is it possible to further delegate the action methods and menu validation from the NSDocument subclass? The best way for me would be to have internalObject implement validateMenuItem and myAction and have the NSDocument pass them along without actually implementing each action method.


It sure is - use invocation forwarding. You need to override the following NSObject methods:

- (NSMethodSignature *)	methodSignatureForSelector:(SEL) aSelector;
- (BOOL)		respondsToSelector:(SEL) aSelector;
- (void)		forwardInvocation:(NSInvocation*) invocation;


The first two methods should query the object you're devolving to when super returns nil and NO respectively, and the third should invoke the invocation on the new target. The result is that the target object can act as if it were directly being targeted by the original command (action) and even implement its own validateMenuItem: etc.


More on this here: http://www.cocoadev.com/index.pl?NSInvocation

It's hard to find in the Apple documentation so I was unable to immediately find the right page there, but it's in there somewhere.

For example, I have a target object referred to as "active layer" and this code exists in my view's controller:

- (void)		forwardInvocation:(NSInvocation*) invocation
{
   SEL aSelector = [invocation selector];

   if ([[self activeLayer] respondsToSelector:aSelector])
       [invocation invokeWithTarget:[self activeLayer]];
   else
       [self doesNotRecognizeSelector:aSelector];
}


- (NSMethodSignature *) methodSignatureForSelector:(SEL) aSelector { NSMethodSignature* sig; sig = [super methodSignatureForSelector:aSelector]; if ( sig == nil ) sig = [[self activeLayer] methodSignatureForSelector:aSelector]; return sig; }


- (BOOL) respondsToSelector:(SEL) aSelector
{
return [super respondsToSelector:aSelector] || [[self activeLayer] respondsToSelector:aSelector];
}



hope this helps,

--Graham




_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >NSDocument Enabling and handling menu items without implementing the action method in the document class (From: Eyal Redler <email@hidden>)
 >Re: NSDocument Enabling and handling menu items without implementing the action method in the document class (From: Graham Cox <email@hidden>)

  • Prev by Date: Re: "intelligent" flexible popup
  • Next by Date: Re: "intelligent" flexible popup
  • Previous by thread: Re: NSDocument Enabling and handling menu items without implementing the action method in the document class
  • Next by thread: How to intercept/reroute API call in loaded plugin?
  • Index(es):
    • Date
    • Thread