我正在構建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
我已經看了周圍,我對如何進行感到不知所措。
我們需要看到創建窗口的代碼 – NSGod
我上面發佈的setupMainWindow方法是創建窗口的代碼 – marpaia
Bah沒有看到滾動條lol – NSGod