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

Windows下的Objective-C集成开发环境(IDE)的搭建 (二)


Windows下的Objective-C集成开发环境(IDE)的搭建 (二)

?

? ? ? ? ? ?继上一步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。图形界面的设计全部在代码中。

?

?

  1. 第一步:使用codeblocks新建一个c的console程序。这里命名该工程名为gui,如图所示:
  2. 添加一个main.c文件,将下面的代码拷贝并覆盖main.c中内容#include "AppController.h"
    1. #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);
      }

    ?

  3. 添加一个AppController.h 头文件,内容如下:
    #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_ */		
  4. 实现一个AppController.h的AppController.m文件#include "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