如何以编程方式将红绿灯按钮添加到OS X应用程序

时间:2014-01-16 22:45:03

标签: objective-c cocoa nswindow appkit

我正在构建OS X cocoa应用程序,并且我没有使用界面构建器(出于各种原因)。我有应用程序到加载菜单,标题栏和主窗口,但我似乎无法弄清楚如何将标志栏按钮添加到标题栏(以编程方式)。

我的AppDelegate.m看起来像这样:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    MainViewController *mVC = [MainViewController new];
    [mVC showMainViewController];
}

然后MainViewController中的代码创建菜单,窗口并加载应用程序,如下所示:

//
//  MainViewController.m
//  TestApp
//

#import "MainViewController.h"

@implementation MainViewController

@synthesize menubar;
@synthesize appMenu;
@synthesize appMenuItem;
@synthesize quitMenuItem;
@synthesize appName;
@synthesize quitTitle;
@synthesize window;
@synthesize homeViewController;
@synthesize resolutionHeight;
@synthesize resolutionWidth;
@synthesize width;
@synthesize height;

- (id)init
{
    self = [super init];
    if (self) {
        appName = @"TestApp";
        [self setupMenubar];
        [self setupMainWindow];
    }
    return self;
}

- (void)setupMenubar
{
    // Set up the main menu
    menubar = [NSMenu new];
    appMenu = [NSMenu new];
    appMenuItem = [NSMenuItem new];
    quitTitle = [NSString stringWithFormat:@"Quit %@", appName];
    quitMenuItem = [[NSMenuItem alloc] initWithTitle:quitTitle
                                              action:@selector(terminate:)
                                       keyEquivalent:@"q"];

    [menubar addItem:appMenuItem];
    [appMenu addItem:quitMenuItem];
    [appMenuItem setSubmenu:appMenu];
    [NSApp setMainMenu:menubar];
}

- (void)setupMainWindow
{
    // set the dimensions of the application
    resolutionWidth = [[NSScreen mainScreen] frame].size.width;
    resolutionHeight = [[NSScreen mainScreen] frame].size.height;

    width = resolutionWidth * 0.75;
    height = resolutionHeight * 0.75;
    window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, width, height)
                                         styleMask:NSTitledWindowMask
                                           backing:NSBackingStoreBuffered defer:NO];
    [window cascadeTopLeftFromPoint:NSMakePoint(10, 10)];

    // add window buttons
    closeButton = [NSWindow standardWindowButton:NSWindowCloseButton forStyleMask:NSTitledWindowMask];


    // set metadata for the window
    [window setTitle:appName];
    [window makeKeyAndOrderFront:nil];
}

- (void)showMainViewController
{
    // Set app settings
    [NSApplication sharedApplication];
    [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
    [NSApp activateIgnoringOtherApps:YES];
    [NSApp run];
}

@end

我环顾四周,对于如何继续,我感到很茫然。

1 个答案:

答案 0 :(得分:2)

您可以尝试将NSWindow init方法更改为以下内容:

window = [[NSWindow alloc]
             initWithContentRect:NSMakeRect(0, 0, width, height)
                       styleMask:NSTitledWindowMask | NSClosableWindowMask |
                        NSMiniaturizableWindowMask | NSResizableWindowMask

                         backing:NSBackingStoreBuffered defer:NO];

我相信其他掩码中的ORing会自动将相应的按钮添加到标题栏:NSClosableWindowMask添加关闭按钮,NSMiniaturizableWindowMask添加最小化(中心)按钮,{{1添加缩放(最右边)按钮。