I'm probably not explaining this logically, as I'm new to Objective-C, but here I go...
I am writing an application in Objective-C that interacts with a WebView. Part of the app involves sharing an image via NSSharingService that is currently displayed in the WebView. Consequently, I have a method like this defined in my AppDelegate.m file:
#import "CCAppDelegate.h" #import <WebKit/WebKit.h> #import <AppKit/AppKit.h> @implementation CCAppDelegate -(void)shareFromMenu:(id)sender shareType:(NSString *)type{ NSString *string = [NSString stringWithFormat: @"window.function('%@')", type]; [self.webView stringByEvaluatingJavaScriptFromString: string]; } @end I then have a subclass of NSMenu, defined in CCShareMenu.m, which creates a menu of available sharing options:
#import "CCShareMenu.h" @implementation CCShareMenu - (void)awakeFromNib{ [self setDelegate:self]; } - (IBAction)shareFromService:(id)sender { NSLog(@"%@", [sender title]); // [CCAppDelegate shareFromMenu]; } - (void)menuWillOpen:(NSMenu *)menu{ [self removeAllItems]; NSArray *shareServicesForItems = @[ [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeMessage], [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail], [NSSharingService sharingServiceNamed:NSSharingServiceNamePostOnFacebook], [NSSharingService sharingServiceNamed:NSSharingServiceNamePostOnTwitter] ]; for (NSSharingService *service in shareServicesForItems) { NSMenuItem *item = [[NSMenuItem alloc] init]; [item setRepresentedObject:service]; [item setImage:[service image]]; [item setTitle:[service title]]; [item setTarget:self]; [item setAction:@selector(shareFromService:)]; [self addItem:item]; } } @end These methods both work fine on their own, except I need to call the shareFromMenu method from within the shareFromService IBAction.
I attempted moving the IBAction method to AppDelegate.m, then realized that made zero sense as the menuWillOpen-created selectors would never find the correct methods. Similarly, I tried following the instructions posted here, but:
[CCAppDelegate shareFromMenu]; Also responded with an error saying that the method was not found.
I realize I'm doing something fundamentally wrong here, so guidance would be appreciated.