将委托设置为我的类的实例?

时间:2012-07-21 16:28:36

标签: c++ objective-c growl

我有一个c ++ / obj c文件集来为Growl创建一种C ++包装器(这是Obj C)但是我被困在一个部分上。我需要在我的Obj C类中设置一个Growl委托,以便调用注册。

这是我的.mm

#import "growlwrapper.h"

@implementation GrowlWrapper
- (NSDictionary *) registrationDictionaryForGrowl {
    return [NSDictionary dictionaryWithObjectsAndKeys:
            [NSArray arrayWithObject:@"Upload"], GROWL_NOTIFICATIONS_ALL,
            [NSArray arrayWithObject:@"Upload"], GROWL_NOTIFICATIONS_DEFAULT
            , nil];
}
@end

void showGrowlMessage(std::string title, std::string desc) {
    std::cout << "[Growl] showGrowlMessage() called." << std::endl;
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [GrowlApplicationBridge setGrowlDelegate: @""];
    [GrowlApplicationBridge
        notifyWithTitle: [NSString stringWithUTF8String:title.c_str()]
        description: [NSString stringWithUTF8String:desc.c_str()]
        notificationName: @"Upload"
        iconData: nil
        priority: 0
        isSticky: YES
        clickContext: nil
    ];
    [pool drain];
}

int main() {
    showGrowlMessage("Hello World!", "This is a test of the growl system");
    return 0;
}

和我的.h

#ifndef growlwrapper_h
#define growlwrapper_h

#include <string>
#include <iostream>
#include <Cocoa/Cocoa.h>
#include <Growl/Growl.h>

using namespace std;

void showGrowlMessage(std::string title, std::string desc);
int main();

#endif

@interface GrowlWrapper : NSObject <GrowlApplicationBridgeDelegate>

@end

现在您可以看到我的[GrowlApplicationBridge setGrowlDelegate: @""];被设置为空字符串,我需要将其设置为某个内容,以便调用registrationDictionaryForGrowl,这当前没有被调用。

但我无法弄明白该怎么做。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

您需要创建GrowlWrapper的实例,并将其作为setGrowlDelegate:方法的委托传递。您只想在应用程序中执行此操作一次,因此每次调用showGrowlMessage时进行设置都不理想。您还希望对此GrowlWrapper保持强有力的参考,以便在完成此操作后将其释放,或者如果您使用ARC则保持有效。因此,从概念上讲,您在启动时需要以下内容:

growlWrapper = [[GrowlWrapper alloc] init];
[GrowlApplicationBridge setGrowlDelegate:growlWrapper];

关机时:

[GrowlApplicationBridge setGrowlDelegate:nil];
[growlWrapper release];    // If not using ARC