相关疑难解决方法(0)

以编程方式创建可可应用程序的初始窗口(OS X)

通常我正在制作iOS应用程序,但现在我正在尝试制作OS X应用程序,而且我在一开始就迷路了.说我制作iOS应用程序的样式完全是程序化的,没有xib文件或者只是因为我通过键入而不是拖动来控制更多.但是在OS X编程中,它从一些带有菜单项和默认窗口的xib文件开始.菜单项中有很多项目,所以可能不是我想乱搞的东西,但我想以编程方式自己创建我的第一个窗口.

所以我这样做了:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{    
    NSUInteger windowStyleMask = NSTitledWindowMask|NSResizableWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask;
    NSWindow* appWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(200, 200, 1280, 720) styleMask:windowStyleMask backing:NSBackingStoreBuffered defer:NO];
    appWindow.backgroundColor = [NSColor lightGrayColor];
    appWindow.minSize = NSMakeSize(1280, 720);
    appWindow.title = @"Sample Window";
    [appWindow makeKeyAndOrderFront:self];
    _appWindowController = [[AppWindowController alloc] initWithWindow:appWindow];
    [_appWindowController showWindow:self];
}
Run Code Online (Sandbox Code Playgroud)

所以在这里,我首先创建了一个窗口,然后使用windowController来初始化这个窗口.窗口确实以这种方式显示,但我只能在这里指定内部元素,如按钮和标签,但不能在windowController中指定.这让我心疼,所以我尝试了另一种方式.

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    _appWindowController = [[AppWindowController alloc] init];
    [_appWindowController showWindow:self];
}
Run Code Online (Sandbox Code Playgroud)

在此之后我想loadWindow:在windowController 中设置函数中的其他元素,如下所示:

- (void)loadWindow
{
    [self.window setFrame:NSMakeRect(200, 200, 1280, 720) display:YES];
    self.window.title = @"Sample window";
    self.window.backgroundColor = [NSColor lightGrayColor]; …
Run Code Online (Sandbox Code Playgroud)

macos cocoa objective-c

21
推荐指数
1
解决办法
2万
查看次数

在Cocoa应用程序中处理Cmd-Q(以编程方式处理菜单项'退出应用程序')

我创建了一个只有一个窗口的游戏应用程序.应用程序是在没有.xib文件帮助的情况下创建的,如下所述:如何创建GUI并以编程方式响应Cocoa事件?

现在,我可以在应用程序的主循环中捕获标准的"键上/下"事件:

 NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES];
 NSEventType eventType = [event type];
 if (eventType == NSKeyDown)
 {
    my_uint32 keycode = [event keyCode];
    input::doSomeWork(keycode);
 }
Run Code Online (Sandbox Code Playgroud)

此外,当使用以下代码在窗口上按下红叉时,我可以正确退出应用程序:

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
    g_myEngine.stop();
    return NSTerminateNow;
}
Run Code Online (Sandbox Code Playgroud)

但我该怎么做:

a)选择菜单项"退出MyApplicationName"时捕获?

b)处理Cmd-Q事件?


更新:我已添加此代码:

id menubar = [[NSMenu new] autorelease];
id appMenuItem = [[NSMenuItem new] autorelease];
[menubar addItem:appMenuItem];
[NSApp setMainMenu:menubar];
id appMenu = [[NSMenu new] autorelease];
id appName = [[NSProcessInfo processInfo] processName];
id quitTitle = [@"Quit " stringByAppendingString:appName];
id quitMenuItem = [[[NSMenuItem …
Run Code Online (Sandbox Code Playgroud)

macos cocoa objective-c

7
推荐指数
1
解决办法
3749
查看次数

标签 统计

cocoa ×2

macos ×2

objective-c ×2