将委托设置为类的实例



我有一组c++/obj c文件来为Growl创建一种c++包装器(即obj c),但我只限于其中一部分。我需要将Growl Delegate设置为Obj C类中的某个对象,以便调用注册。

这是我的.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,而它目前没有被调用。

但我不知道怎么做。有什么帮助吗?

您需要创建一个GrowlWrapper的实例,并将其作为委托传递给setGrowlDelegate:方法。您只想在应用程序中执行一次,因此每次调用showGrowlMessage时都设置它并不理想。您还需要保留对这个GrowlWrapper的强烈引用,这样您就可以在使用完它后发布它,或者在使用ARC时保持有效。因此,从概念上讲,您在启动时需要以下内容:

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

关闭时:

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

最新更新