2013-02-25 78 views
0

我尝试了我的第一个Hello World GUI应用程序在Cocoa中,而无需使用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.菜单栏上没有显示任何内容。

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

回答

1

一旦你点击了“x”按钮,你的应用程序就会收到通知。

之后,您不能回滚到不关闭。

所以这是这里的问题。

但是,您可以用基于文档的应用程序完成这种工作。

一定有什么东西,但将被视为黑客停止发送到应用程序和操作系统

+0

感谢您的回答通知。但我一个接一个地看到消息框? – doptimusprime 2013-02-25 12:04:45

+0

另外,我想知道我的代码中是否有任何错误。我也没有在菜单栏上找到任何东西。 – doptimusprime 2013-02-25 12:36:45

+1

你无法做任何事情......一旦你给出一个命令关闭,它将100%完成,这就是为什么你得到msgbox..msgbox..msgbox ...反复/ – 2013-02-25 13:32:59

相关问题