【问题标题】:Call AppDelegate Method From Subclass?从子类调用 AppDelegate 方法?
【发布时间】:2014-06-29 06:43:34
【问题描述】:

我可能没有从逻辑上解释这一点,因为我是 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

然后我有一个NSMenu 的子类,在CCShareMenu.m 中定义,它创建了一个可用共享选项的菜单:

#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

这些方法都可以自己正常工作,除了我需要从shareFromServiceIBAction 中调用shareFromMenu 方法。

我尝试将IBAction 方法移动到AppDelegate.m,然后意识到这是零意义,因为menuWillOpen 创建的选择器永远找不到正确的方法。同样,我尝试按照here 发布的说明进行操作,但是:

[CCAppDelegate shareFromMenu];

还回应了一个错误,说没有找到该方法。

我意识到我在这里做的根本性错误,因此我们将不胜感激。

【问题讨论】:

    标签: objective-c cocoa


    【解决方案1】:

    -(void)shareFromMenu 是成员方法,但是

    [CCAppDelegate shareFromMenu]

    正在调用一个类函数,这不是调用成员函数的正确方法。

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

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

    【讨论】:

      【解决方案2】:

      -[CCAppDelegate shareFromMenu]

      不同于

      -[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 是有效的,你能解释一下为什么吗?
      • 头文件中声明的方法是公共的,因此导入该文件的其他类可以访问。在实现文件中声明的方法是该类私有的。
      • 如果我不使用- 标志我使用+ 标志,它们甚至是私有的吗? - void()+ void()?
      • 没错。 + 表示类方法,- 表示实例方法。它们与范围无关。另见this questionthis tutorial
      • 我明白了。我因错误地阅读 this answer 而感到困惑。感谢您的帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 2021-03-15
      • 2014-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多