Objective c 是否从子类调用AppDelegate方法?

Objective c 是否从子类调用AppDelegate方法?,objective-c,cocoa,Objective C,Cocoa,我可能没有从逻辑上解释这一点,因为我对Objective-C还不熟悉,但现在我开始 我正在用Objective-C编写一个与WebView交互的应用程序。应用程序的一部分包括通过NSSharingService共享当前显示在WebView中的图像。因此,我在我的AppDelegate.m文件中定义了这样一个方法: #import "CCAppDelegate.h" #import <WebKit/WebKit.h> #import <AppKit/AppKit.h> @

我可能没有从逻辑上解释这一点,因为我对Objective-C还不熟悉,但现在我开始

我正在用Objective-C编写一个与
WebView
交互的应用程序。应用程序的一部分包括通过
NSSharingService
共享当前显示在
WebView
中的图像。因此,我在我的
AppDelegate.m
文件中定义了这样一个方法:

#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
这些方法本身都可以正常工作,但我需要从
shareFromService
iAction
中调用
shareFromMenu
方法

我尝试将
iAction
方法移动到
AppDelegate.m
,然后意识到这毫无意义,因为
menuWillOpen
创建的选择器永远找不到正确的方法。类似地,我尝试按照张贴的说明进行操作,但:

也回答了一个错误,说找不到该方法


我意识到我在这里犯了一些根本性的错误,因此希望您能给予指导。

-[CCAppDelegate sharefromfmenu]

不同于


-[CCAppDelegate shareFromMenu:shareType:]

我将尝试在
@interface
@end
之间向CCAppDelegate.h添加以下内容:

-(void)shareFromMenu:(id)sender shareType:(NSString *)type
然后将您的
shareFromService:
方法更改为类似以下内容:

- (IBAction)shareFromService:(id)sender
{
    NSString *shareType = @"Set your share type string here.";

    CCAppDelegate *appDelegate = (CCAppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate shareFromMenu:sender shareType:shareType];
}
-(void)shareFromMenu是成员方法,但

[CCAppDelegate shareFromMenu]

调用类函数不是调用成员函数的正确方法

您可以尝试获取CCAppDelegate实例,然后像这样调用函数

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

[appDelegate shareFromMenu]

我的错误是忘记了方法中的参数;我忘了复制和粘贴所有东西。在头文件中添加
void
,但是,您能解释一下为什么吗?头文件中声明的方法是公共的,因此导入该文件的其他类可以访问这些方法。实现文件中声明的方法对于该类是私有的。如果我使用
+
符号而不是
-
符号,它们是否私有<代码>-void()
vs
+void()
?这是正确的<代码>+表示类方法,
-
表示实例方法。它们与范围无关。也看到了,我看到了。我因为阅读错误而感到困惑。谢谢你的帮助。
-(void)shareFromMenu:(id)sender shareType:(NSString *)type
- (IBAction)shareFromService:(id)sender
{
    NSString *shareType = @"Set your share type string here.";

    CCAppDelegate *appDelegate = (CCAppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate shareFromMenu:sender shareType:shareType];
}