2013-04-11 74 views
2

有沒有一種方法在obj-c中創建一個gui窗口,而沒有.app包,只是extcutable?問題是我需要一個gui應用程序(只有一個對話框)。Objective-c如何從終端應用程序製作GUI窗口

我試過了: 將appkit.framework導入終端應用程序。 - 崩潰有很多原因。 我決定使用x11不是一個好主意,因爲它不包含在OS X 10.8中

那麼,有什麼想法?

+0

如果您可以將理由添加到您的問題 – 2013-04-11 20:25:28

+0

通常的方法是象徵性地將「命令行應用程序」作爲符號鏈接鏈接到應用程序包內的可執行文件。 Mac OS X啓動器/擴展塢需要捆綁。如果您遇到捆綁問題,只需將捆綁包隱藏在別人看不到的地方(比如不在/應用程序中),那麼有什麼區別?你真的需要確保人們不能雙擊你的東西嗎? – 2013-04-12 00:52:58

回答

5

不幸的是,你需要運行一個NSApplication,但你不需要有一個應用程序包。

確保在創建項目時選擇「基礎」作爲類型。然後你可以像這樣設置它:

#import <Foundation/Foundation.h> 
#import <AppKit/AppKit.h> 

@interface MyAppDelegate : NSObject <NSApplicationDelegate> 
{ 
    NSWindow *window; 
} 
@end 

@implementation MyAppDelegate 

-(id) init 
{ 
    self = [super init]; 
    if (self) 
    { 
     // total *main* screen frame size // 
     NSRect mainDisplayRect = [[NSScreen mainScreen] frame]; 

     // calculate the window rect to be half the display and be centered // 
     NSRect windowRect = NSMakeRect(mainDisplayRect.origin.x + (mainDisplayRect.size.width) * 0.25, 
            mainDisplayRect.origin.y + (mainDisplayRect.size.height) * 0.25, 
            mainDisplayRect.size.width * 0.5, 
            mainDisplayRect.size.height * 0.5); 

     /* 
     Pick your window style: 
     NSBorderlessWindowMask 
     NSTitledWindowMask 
     NSClosableWindowMask 
     NSMiniaturizableWindowMask 
     NSResizableWindowMask 
     */ 
     NSUInteger windowStyle = NSTitledWindowMask | NSMiniaturizableWindowMask; 

     // set the window level to be on top of everything else // 
     NSInteger windowLevel = NSMainMenuWindowLevel + 1; 

     // initialize the window and its properties // just examples of properties that can be changed // 
     window = [[NSWindow alloc] initWithContentRect:windowRect styleMask:windowStyle backing:NSBackingStoreBuffered defer:NO]; 
     [window setLevel:windowLevel]; 
     [window setOpaque:YES]; 
     [window setHasShadow:YES]; 
     [window setPreferredBackingLocation:NSWindowBackingLocationVideoMemory]; 
     [window setHidesOnDeactivate:NO]; 
     [window setBackgroundColor:[NSColor greenColor]]; 
    } 
    return self; 
} 

- (void)applicationWillFinishLaunching:(NSNotification *)notification 
{ 
    // make the window visible when the app is about to finish launching // 
    [window makeKeyAndOrderFront:self]; 
    /* do layout and cool stuff here */ 
} 

- (void)applicationDidFinishLaunching:(NSNotification*)aNotification 
{ 
    /* initialize your code stuff here */ 
} 

- (void)dealloc 
{ 
    // release your window and other stuff // 
    [window release]; 
    [super dealloc]; 
} 

@end 


int main(int argc, const char * argv[]) 
{ 

    @autoreleasepool 
    { 
     NSApplication *app = [NSApplication sharedApplication]; 
     [app setDelegate:[[[MyAppDelegate alloc] init] autorelease]]; 
     [app run]; 
    } 
    return 0; 
} 

請記住,[app run]; [被阻塞,所以你將不得不在應用程序循環或新線程(首選應用程序循環)中運行你的邏輯。

希望它有幫助。

編輯:噓......我太遲了!

+0

只是爲了擴展@Chris Devereux的答案,做完這些之後,您不必設置運行循環,因爲[app run]行[實際上會爲您創建一個運行循環。在這一點上,你可以將你的應用程序*幾乎*視爲你有捆綁軟件。 – Dario 2013-04-11 21:00:04

+1

噢男人,先回答。無論如何歡迎來到!通常情況下,問題可以很快得到解答,因此避免使用長代碼樣本是一個好主意,除非真的需要它們(或者稍後再編輯它們)。 – 2013-04-11 21:17:10

0

我想你應該只是製作一個可可應用程序,只需將你需要的文件拖放到新的應用程序中。確保勾選「添加到目標」和「將項目複製到目的地......」。

我已經做了許多這些小幫手應用程序,只是爲了幫助我完成一些簡單的任務。這通常是我這樣做的方式。

+0

我想你不明白我的問題。問題是我無法制作任何.app軟件包。我需要將所有內容硬編碼到可執行文件中。但NSApplication需要捆綁。所以我正在尋找一種方法去做& – Maxim 2013-04-11 20:25:28

1

你所描述的應該是可能的,不需要創建一個.app,但是你需要運行AppKit事件循環(否則,用戶輸入事件會從哪裏來?)。

最簡單的方法是獲取共享的NSApplication對象並調用它的run方法。您可以使用nsrunloop手動設置事件循環,但我不確定輸入源是否需要公共API。

爲什麼你不想要一個.app?如果你遵循標準模式,那麼無論你做什麼都會更容易。

相關問題