我正在构建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