2014-01-16 78 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

相關問題