我在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行为。
答案 0 :(得分:1)
点击“x”按钮后,系统会向您的应用发送通知以便关闭。
此后你无法回滚而不关闭。
所以这就是问题所在。
但是,您可以使用基于文档的应用程序轻松完成此类工作。
必须有一些东西,但这将被视为黑客停止发送到应用程序和操作系统的通知