2012-07-21 28 views
0

我有一個C++/obj c文件集來爲Growl(這是Obj C)創建一種C++包裝,但是我被困在一個部分。我需要在我的Obj C類中設置一個Growl Delegate,以便註冊被調用。將委託設置爲我班的實例?

這是我.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被調用,目前沒有被調用。

但我不知道該怎麼做。任何幫助?

回答

0

您需要創建一個GrowlWrapper的實例並將其作爲代理傳遞給setGrowlDelegate:方法。您只想在應用程序中這樣做一次,因此每次撥打電話showGrowlMessage都不太理想。您還需要保留對這個GrowlWrapper的強烈參考,以便在完成之後釋放它,或者在使用ARC時保持有效。所以,在概念上,你會想是在啓動時執行以下操作:

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

而且在關機:

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

我得到一個錯誤'growlwrapper.mm:15:錯誤:「growlWrapper」在未聲明這個範圍(這一行是GrowlWrapper的分配線,我需要將growlWrapper添加到我的.h文件中嗎?對不起,我是一個noob :( – Steven 2012-07-21 17:02:10

+0

是的,你需要在某處聲明growlWrapper。最簡單的東西如果你只是想讓你的測試工作就是在你分配/初始化之前插入'GrowlWrapper * growlWrapper'。 – 2012-07-21 17:10:29