objective-c - 单击取消按钮时出现消息框

标签 objective-c cocoa

我在 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. 单击关闭按钮后,会显示一个消息框。单击"is"后,它将退出。但是,单击“否”或“取消”后,它会一次又一次地显示消息框,除非我单击"is"。 2. 菜单栏不显示任何内容。

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

最佳答案

单击“x”按钮后,您的应用程序就会收到关闭通知。

此后您将无法回滚至不关闭。

所以这就是问题所在。

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

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

关于objective-c - 单击取消按钮时出现消息框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15065712/

相关文章:

cocoa - NSZombies 正在吃我应用程序的大脑!

ios - 传递数据 VC 委托(delegate) iOS

iphone - NSFileManager 多实例写入原子性

ios - 应用程序在 `once.h` 定义的 SIGABRT 中崩溃

cocoa - 在 Cocoa 项目上使用版本控制的最佳实践

macos - NSCollectionView 选择和取消选择

ios - 将 UINavigationBar alpha 更改为 0,然后返回主页并返回应用程序。 navigationBar alpha 更改为 1

objective-c - 反转 NSArray 顺序的简便方法

iphone - didRegisterForRemoteNotificationsWithDeviceToken - 推送通知

objective-c - 从两个指针数组中检索关系