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.

Community
  • 1
  • 1
Charlie
  • 11,380
  • 19
  • 83
  • 138

2 Answers2

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];
}
CyberMoai
  • 496
  • 3
  • 11
  • 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? – Charlie May 12 '14 at 02:56
  • 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. – CyberMoai May 12 '14 at 03:11
  • Are they even private if instead of using a `-` sign I use a `+` sign? `- void()` vs `+ void()`? – Charlie May 12 '14 at 03:17
  • That is correct. `+` denotes a class method while `-` denotes an instance method. They have nothing to do with scope. See also [this question](http://stackoverflow.com/questions/1053592/what-is-the-difference-between-class-and-instance-methods) and [this tutorial](http://rypress.com/tutorials/objective-c/classes.html). – CyberMoai May 12 '14 at 03:22
  • I see. I was getting that confused from reading [this answer](http://stackoverflow.com/questions/1658433/accessing-method-from-other-classes-objective-c) incorrectly. Thanks for the help. – Charlie May 12 '14 at 03:28
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];

boycechang
  • 11
  • 2