2013-02-25 82 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

相關問題