2014-11-20 82 views
1

我试图创建一个简单的应用程序,只是打开一个警报。所以,想象这在命令行中创建警报/消息框OSX应用程序

int main(int argc, const char * argv[]) { 
    int result = SomeMagicAlertFunction("Hello World", "Yes", "No"); 
    printf("User picked: %d¥n", result); 
} 

我发现了一些信息关于NSAlert但所有的例子都是全OSX应用,那种与应用程序包中

+-MyApp.app 
    | 
    +-Contents 
    | 
    +-MacOS 
     | 
     +-MyApp 

等,但我只想要一个在命令行应用程序中提醒。一个文件,而不是一个应用程序包。在C/C++或Objective C中,这在OSX中可能吗?我看到了一些关于NSRunAlertPanel的东西,但在优胜美地里已被删除,并说要使用NSAlert

回答

4

找到答案片刻之后

#import <Cocoa/Cocoa.h> 

void SomeMagicAlertFunction(void) { 
    NSAlert *alert = [[NSAlert alloc] init]; 
    [alert addButtonWithTitle:@"OK"]; 
    [alert addButtonWithTitle:@"Cancel"]; 
    [alert setMessageText:@"Delete the record?"]; 
    [alert setInformativeText:@"Deleted records cannot be restored."]; 
    [alert setAlertStyle:NSWarningAlertStyle]; 

    if ([alert runModal] == NSAlertFirstButtonReturn) { 
    } 
    //[alert release]; 
} 
+1

FWIW,如果你只是想显示一条消息未经整理保持程序,你可以使用'CFUserNotificationDisplayNotice'。 – JWWalker 2014-11-20 20:19:01