0

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.

2 Answers 2

1

-[CCAppDelegate shareFromMenu]

is different from

-[CCAppDelegate shareFromMenu:shareType:]

I would try adding the following to CCAppDelegate.h between @interface and @end:

-(void)shareFromMenu:(id)sender shareType:(NSString *)type 

Then change your shareFromService: method to something like:

- (IBAction)shareFromService:(id)sender { NSString *shareType = @"Set your share type string here."; CCAppDelegate *appDelegate = (CCAppDelegate *)[[UIApplication sharedApplication] delegate]; [appDelegate shareFromMenu:sender shareType:shareType]; } 
Sign up to request clarification or add additional context in comments.

5 Comments

My fault on forgetting the parameter in the method; I neglected to copy and paste everything. Adding the void in the header file worked however, can you explain why though?
Methods declared in the header file are public and therefore accessible to other classes that import said file. Methods declared in the implementation files are private to that class.
Are they even private if instead of using a - sign I use a + sign? - void() vs + void()?
That is correct. + denotes a class method while - denotes an instance method. They have nothing to do with scope. See also this question and this tutorial.
I see. I was getting that confused from reading this answer incorrectly. Thanks for the help.
1

-(void)shareFromMenu is a member method, but

[CCAppDelegate shareFromMenu]

is calling a class function which is not the correct way to call a member function.

You may try to get the CCAppDelegate instance and then call the function like this

CCAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; [appDelegate shareFromMenu];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.