 |
 |
dock menu
|
 |
|
 |
|
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status:
Offline
|
|
so... i got a small application that dynamically creates a dock menu. i set action and target, they get called. great so far.
but if i call [sender representedObject], all i get is "*** -[NSApplication representedObject]: selector not recognized". why is the sender an nsapplication and no nsmenuitem? how do i find out which of my menu items has been selected by the user...?
a little clueless...
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
Originally posted by seb2:
<STRONG>so... i got a small application that dynamically creates a dock menu. i set action and target, they get called. great so far.
but if i call [sender representedObject], all i get is "*** -[NSApplication representedObject]: selector not recognized". why is the sender an nsapplication and no nsmenuitem? how do i find out which of my menu items has been selected by the user...?
a little clueless...</STRONG>
I usually just do it by title, but maybe that's just because I'm lazy  Perhaps you can do it by index too? I'm not exactly sure why your sender ends up being an NSApplication object, but I know the dockMenu is an outlet of NSApplication, so that could make some sense...
F-bacher
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Mar 2001
Status:
Offline
|
|
Originally posted by seb2:
<STRONG>so... i got a small application that dynamically creates a dock menu. i set action and target, they get called. great so far.
but if i call [sender representedObject], all i get is "*** -[NSApplication representedObject]: selector not recognized". why is the sender an nsapplication and no nsmenuitem? how do i find out which of my menu items has been selected by the user...?
a little clueless...</STRONG>
The DockMenu is a contextual menu, that means that when an item is selected, the receiver of the event is not the application, but some systemwide contextual menu observer (I suppose).
The message may be relayed to the Application that sends the action. However, the sender is not the menuItem, but the Application Instance.
Hope this helps.
|
|
Imagine that my signature is here...
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 2000
Location: Glasgow
Status:
Offline
|
|
What that error is telling you is that the 'sender' that is passed to the target actions of dock menus is actually the global NSApplicaiton instance, and not the NSMenuItem.
This means that the Dock (or whatever magicode actually makes the calls) is passing NSApp as that sender parameter rather than the UI widget triggering the action (which is kind of how most other stuff in cocoa works).
I believe this is either a bug or a design flaw in the Dock Menu API. I think it's an indicator of a slightly, um, rushed API implementation.
Here's a workaround from Brian Webster (of Docklet implementation fame):
Well, I did make a workaround for it, and I'd say it's rather "interesting". I gave each menu item a different action, the same name with a number tacked on to the end of it (doStuff1:, doStuff2:, etc.) I then implemented forwardInvocation: (and methodSignatureForSelector: of course) to pick the number off of the end of the selector so I could tell which menu item had been chosen. Very very ugly... but it worked!
HTH,
Fraz
|
|
PowerBook G4 17"
Power Mac G4/800, 1Gb RAM, 80Gb HDD, Superdrive, GeForce 4MX, Gateway 21" CRT, Apple Pro Speakers, iSub - Running Mac OS X Server 10.2
iBook 500, 192MbRAM - Running Mac OS X 10.2
iPod 5Gb
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status:
Offline
|
|
found a workaround i consider "more elegant":
created a class that is able to remeber a menuitems and each menuitem is given another of those target objects that remembers the command associated with the respective menuitem... phew... works
thank you anyway for the help. anybody interested in the gui wrapper for "locate" i now finished?
[ 11-01-2001: Message edited by: seb2 ]
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
Could someone post a code snipped of what they did to get around this... maybe I'm just confused.
But here's some very disturbing info. According to the docs, when you click on an NSMenuItem it sends your instance of NSApplication a sendActionToTargetFromSender message. So, a good way to fix what ever is getting messed up would seem to be subclassing NSApplication and overriding this method with a new implementation. Well, after doing so, I found something distrubing: the method was never called! I know everything is setup right because I can call the method manually off of me NSApplication instance and it prints out my nice "Why does apple's docs suck?" message nicely in the log, but it never gets called when I select my menu item in the dock menu. I know the menu items have properly set up selectors, for they do call the right methods, but don't pass in anything useful. Whose idea in apple was it that sending in an instance of NSApplication was more useful than the actual object that started the message? He deserves to be canned
What a way to get your 500th post...
F-bacher
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status:
Offline
|
|
ok, i really don't know whether this is elegant or not, but at least it works...
i wrote a small class i called MenuExecutor, its .h looks like this:
<font face = "courier">#import <Foundation/Foundation.h>
#import "XLController.h"
@interface MenuExecutor : NSObject {
NSString *path;
XLController *owner;
}
- (id)initWithOwner  XLController *)xl;
- (void)setMyPath  NSString *)p;
- (void)execute  id)sender;
@end
</font>... its .m looks like this:<font face = "courier">
#import "MenuExecutor.h"
@implementation MenuExecutor
- (id)initWithOwner  XLController *)xl
{
[super init];
path = [NSString alloc];
owner = xl;
return self;
}
- (void)execute  id)sender
{
[owner itemSelected  ath];
}
- (void)setMyPath  NSString *)p
{
[path autorelease];
path = p;
[path retain];
}
@end[tt]
then, when i create a dock menu item, i set do this:
[tt] NSMenuItem *tmpItem;
...
tmpItem = [[[NSMenuItem alloc] initWithTitle:[showFiles objectAtIndex:i] action:@selector(execute  keyEquivalent:@""] autorelease];
[tmpItem setTarget:[mExecutors objectAtIndex:i]];
[dynamicMenu addItem:tmpItem];
[[mExecutors objectAtIndex:i] setMyPath:[showFiles objectAtIndex:i]];
</font>
as you might have guessed, i have an array with "MenuExecutors" in it which is called "mExecutors". finally, in the "owner" of the MenuExecutor, i have a method
<font face = "courier">- (void)itemSelected  NSString *)which</font>
which gets passed the argument i had passed to the menuExecutor by "setMyPath" and i can do whatever i want to there.
phew.
this definitively is not very nice and i agree that the person that invented that mechanism should be fired, but, whatever...
the "init" doesn't take a path as its argument because i "recycle" the menuexecutors and that way every time i need a new argument just call its "setMyPath" and don't have to do with deallocating and reallocating; the array is once set up when the app starts.
oh, btw, this is from a small application i wrote, "locator".
if you have questions about the code, feel free to ask me.
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status:
Offline
|
|
sorry for that one (opening bracket)tt(closing bracket) in the post above.
for some reason when i try to edit the post i get "We cannot process your posting, because you have exceeded the maximum number of images allowed per post. The current maximum is 8."
hmm, my count comes close to zero. but otoh i wasn't able to come up with a more elegant solution. guess it's my fault. 
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|