Objective-c如何从终端应用程序制作GUI窗口

Max*_*xim 2 macos user-interface xcode objective-c

在obj-c中是否有一种方法可以创建一个gui窗口,而没有.app包,只是可以解压缩?问题是我需要一个gui应用程序(只有一个对话框).

我试过:将appkit.framework导入终端应用程序. - 崩溃有很多原因.我决定使用x11不是一个好主意,因为它不包含在OS X 10.8中

那么,有什么想法吗?

Dar*_*rio 5

遗憾的是,您需要运行NSApplication,但您不需要拥有应用程序包.

确保在创建项目时选择"Foundation"作为类型.然后你可以这样设置:

#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;
}
Run Code Online (Sandbox Code Playgroud)

请记住该行[app run]; 阻塞所以你必须在应用程序循环或新线程(首选的应用程序循环)上运行你的逻辑.

希望能帮助到你.

编辑:嘘......我来不及了!