日期:2014-05-17 浏览次数:21350 次

?
? ? ? ? ? ?继上一步Windows下的Objective-C集成开发环境(IDE)的搭建 (一)配置运行命令行程序后,今天来讲解一下如何使用
codeblocks配置开发使用cocoa framework开发GUI程序。
?
#include "AppController.h"
#include <AppKit/AppKit.h>
int main(int argc, const char *argv[])
{
   NSAutoreleasePool *pool;
   AppController *delegate;
   pool = [[NSAutoreleasePool alloc] init];
   delegate = [[AppController alloc] init];
   [NSApplication sharedApplication];
   [NSApp setDelegate: delegate];
   RELEASE(pool);
   return NSApplicationMain (argc, argv);
}
?
?
我们使用一个helloworld开始旅程。
?
?
? ? 这个helloworld程序共有五个文件:main.m、AppController.h、AppController.m、helloworldInfo.plist和GNUmakefile。图形界面的设计全部在代码中。
?
?
 
#include <AppKit/AppKit.h>
int main(int argc, const char *argv[])
{
   NSAutoreleasePool *pool;
   AppController *delegate;
   pool = [[NSAutoreleasePool alloc] init];
   delegate = [[AppController alloc] init];
   [NSApplication sharedApplication];
   [NSApp setDelegate: delegate];
   RELEASE(pool);
   return NSApplicationMain (argc, argv);
}
?
#ifndef _AppController_H_
#define _AppController_H_
#include <Foundation/NSObject.h>
@class NSWindow;
@class NSTextField;
@class NSNotification;
@interface AppController : NSObject
{
   NSWindow *window;
   NSTextField *label;
}
- (void)applicationWillFinishLaunching:(NSNotification *) not;
- (void)applicationDidFinishLaunching:(NSNotification *) not;
@end
#endif /* _AppController_H_ */		
#include <AppKit/AppKit.h>
@implementation AppController
- (void) applicationWillFinishLaunching: (NSNotification *) not
{
   /* Create Menu */
   NSMenu *menu;
   NSMenu *info;
   menu = [NSMenu new];
   [menu addItemWithTitle: @"Info"
                   action: NULL
            keyEquivalent: @""];
   [menu addItemWithTitle: @"Hide"
                   action: @selector(hide:)
            keyEquivalent: @"h"];
   [menu addItemWithTitle: @"Quit"
                   action: @selector(terminate:)
            keyEquivalent: @"q"];
   info = [NSMenu new];
   [info addItemWithTitle: @"Info Panel..."
                   action: @selector(orderFrontStandardInfoPanel:)
            keyEquivalent: @""];
   [info addItemWithTitle: @"Preferences"
                   action: NULL 
            keyEquivalent: @""];
   [info addItemWithTitle: @"Help"
                   action: @selector (orderFrontHelpPanel:)
            keyEquivalent: @"?"];
   [menu setSubmenu: info 
            forItem: [menu itemWithTitle:@"Info"]];
   RELEASE(info);
   [NSApp setMainMenu:menu];
   RELEASE(menu);
   /* Create Window */
   window = [[NSWindow alloc] initWithContentRect: NSMakeRect(300, 300, 200, 100)
                              styleMask: (NSTitledWindowMask |
                                          NSMiniaturizableWindowMask |
                                          NSResizableWindowMask)
                               backing: NSBackingStoreBuffered
                               defer: YE