0

我在 Cocoa 中尝试了我的第一个 Hello World GUI 应用程序,而没有使用 Interface Builder(因为我只需要以这种方式进行操作)。在此,我编写以下代码。

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
 NSWindow * vWindow;     ///< Window under delegation.
 //BOOL       vQuit;       ///< Flag to inidicate wheather to quit or not.
}
@property (assign) IBOutlet NSWindow *window;
- (id) init;
- (void) CreateWindow;
@end

//Implementation.
#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = vWindow;

-(id)init
{
     if(self = [super init]) {
       //my allocation if required.
      }

      return self;
}

- (void)dealloc
{
     [super dealloc];
}


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
     // Insert code here to initialize your application
     [vWindow setTitle:@"Hello World Application"];
}

- (void)applicationWillTerminate:(NSNotification *)notification
{
    [self dealloc];
}

-(NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
     return 1;
}

/**
 This is called when the last window is closed.
 */
 -(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
 {    
         int answer;

    answer  = NSRunAlertPanel(@"Quit Confirmation", @"Do you want it to close?", @"Yes", @"No", @"Cancel");
    if(NSAlertDefaultReturn == answer)
        return YES;

    //Recreate the Windows.
    [self CreateWindow];
    return NO;
 }


/**
 * It creates the Window and assign it to the current Window.
*/
-(void)CreateWindow
{
    //Adding the menu bar.
    id      menubar;        ///< Menu bar.

//Create new menubar.
menubar     = [[[NSMenu alloc] initWithTitle:@"Menu"] autorelease];

    //Add a menu item to the menubar.
    //[menubar addItem:menuitem];

    id      quitmenu;   ///< quit menu.

quitmenu    = [[[NSMenuItem alloc] initWithTitle:@"Quit"
                                          action:@selector(terminate) keyEquivalent:@"q"]
               autorelease];

[menubar addItem:quitmenu];


//Set the main menu
[NSApp setMainMenu:menubar];

//Add close menu.
    id      window;     ///< Window.

window  = [[[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 200, 200)
                                       styleMask:NSResizableWindowMask|NSTitledWindowMask|NSClosableWindowMask
                                         backing:NSBackingStoreBuffered defer:NO] autorelease];

//where to mention my delegate.
[window cascadeTopLeftFromPoint:NSMakePoint(20,20)];
[window setTitle:@"Hello World App"];
[window makeKeyAndOrderFront:nil];

//Let us print "Hello Mac OS X" on this Windows.
    NSString *    strmessage;

strmessage  = [[NSString alloc] initWithUTF8String:"Hello Mac OS X"];

[strmessage drawAtPoint:NSMakePoint(30, 30) withAttributes:nil];

//Let us add few control (in assignment 2)

[self setWindow:window];

//make this window a key window
[window makeKeyAndOrderFront:nil];
}
@end

int main(int argc, char ** argv)
{
    NSApplication * application;    ///< application.
    AppDelegate *   appdelegate;    ///< Application delegate.

  //setup new auto release pool.
  [NSAutoreleasePool new];

  //Instantiate the instance of application.
  application     = [NSApplication sharedApplication];

  //Set the delegate here.
  appdelegate     = [[AppDelegate alloc] init];
  [application setDelegate:appdelegate];

  //Create Window on delegate.
  [appdelegate CreateWindow];


  //Start of the message loop.
  [NSApp run];


  //Any cleanup after this.

  [appdelegate release];
  [application release];

  return 0;
}

我面临以下问题: 1. 单击关闭按钮时,会显示一个消息框。单击是时,它会退出。但是,单击“否”或“取消”时,它会一次又一次地显示消息框,除非我单击“是”。2. 菜单栏无任何显示。

请让我知道我在这里缺少什么。如果有任何没有 nib 或 xib 文件的学习 Cocoa 应用程序的好链接,请发布。我还需要支持其他功能,例如 Command-W 和 Command-Q 行为。

4

1 回答 1

1

一旦您点击了“x”按钮,您的应用就会收到关闭通知。

在此之后你不能回滚到不关闭。

所以这就是这里的问题。

但是,您可以使用基于文档的应用程序非常轻松地完成此类工作。

必须有一些东西,但这将被视为阻止发送到应用程序和操作系统的通知的黑客行为

于 2013-02-25T11:57:03.330 回答