2014-01-16 83 views
0

我正在构建OS X可可应用程序,并且我没有使用接口生成器(出于各种原因)。我已经得到了加载菜单,标题栏和主窗口的应用程序,但我似乎无法弄清楚如何将红灯按钮添加到标题栏(以编程方式)。如何以编程方式将Stoplight按钮添加到OS X应用程序

我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 

我已经看了周围,​​我对如何进行感到不知​​所措。

+0

我们需要看到创建窗口的代码 – NSGod

+0

我上面发布的setupMainWindow方法是创建窗口的代码 – marpaia

+0

Bah没有看到滚动条lol – NSGod

回答

2

你可以试着改变你的NSWindow init方法为以下内容:

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

         backing:NSBackingStoreBuffered defer:NO]; 

相信的ORing在额外的掩模会自动添加相应的按钮,标题栏为您提供:NSClosableWindowMask增加了关闭按钮,NSMiniaturizableWindowMask添加最小化(中心)按钮,NSResizableWindowMask添加缩放(最右边)按钮。

+1

你真的是NSGod;)非常感谢你! – marpaia

+0

扩大一点。 NSWindow方法standardWindowButton:只返回对系统提供的标准按钮的引用。它不创建它们,并且除了提供正确的NSWindowStyleMasks之外,可可中没有公共API来创建它们。如果您使用自定义按钮创建自定义窗口,则需要将它们连接到右侧选择器,并处理活动,非活动,不可用,非关键和鼠标进入状态的更改mouseExited和mouseDown以及可能文档不清晰 – uchuugaka

+0

从技术上讲,您可以使用这些方法以获得按钮,但他们不会正确连接或视觉上对状态变化作出反应。它可能是一个API错误或文档错误。 – uchuugaka

相关问题