使用NSMenuItems在其中创建NSMenu,以编程方式? [英] Creating NSMenu with NSMenuItems in it, programmatically?

查看:165
本文介绍了使用NSMenuItems在其中创建NSMenu,以编程方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想指出,这个问题可能已经问过了,我找不到任何答案。

First, I'd like to point out that this question is probably already asked, I just couldn't find any answers from them.

m以编程方式试图创建一个NSMenu和NSMenuItem到主栏,所以fe。 NSMenu将是文件,然后它将有3x NSMenuItem在其中,新,打开和保存。

So, I'm programmatically trying to create a NSMenu and NSMenuItem to the main bar, so fe. NSMenu would be File and then it would have 3x NSMenuItem in it, New, Open and Save.

但没有什么工作,这里是我目前:

But nothing's working, here's what I have currently:

NSMenu *fileMenu = [[NSMenu alloc] initWithTitle:@"File"];
NSMenuItem *newMenu = [[NSMenuItem alloc] initWithTitle:@"New" action:NULL keyEquivalent:@""];
NSMenuItem *openMenu = [[NSMenuItem alloc] initWithTitle:@"Open" action:NULL keyEquivalent:@""];
NSMenuItem *saveMenu = [[NSMenuItem alloc] initWithTitle:@"Save" action:NULL keyEquivalent:@""];
[newMenu setMenu:fileMenu];
[openMenu setMenu:fileMenu];
[saveMenu setMenu:fileMenu];

但没有什么发生,我很确定我必须告诉应用程序应该使用fileMenu,但是如何做到这一点,如果这不是问题,那么什么是?我对这个东西很新鲜,但对学习感兴趣,所以只要任何提示就会比没有东西更好。

But nothing happens, I'm pretty sure I have to tell the application that it should use fileMenu, but how do I do that, and if that's not the problem, then what is? I'm pretty new at this stuff, but interested in learning, so just any tip will be better than nothing!

推荐答案

要将这三个项目添加到菜单中,请使用:

To add those three items to your menu, use:

[fileMenu addItem: newMenu];
[fileMenu addItem: openMenu];
[fileMenu addItem: saveMenu];

然后将菜单添加到菜单栏:

And then to add the menu to the menu bar:

NSMenuItem *fileMenuItem = [[NSMenuItem alloc] initWithTitle: @"File"];
[fileMenuItem setSubmenu: fileMenu]; // was setMenu:
[[NSApp mainMenu] addItem: fileMenuItem];
[fileMenuItem release];

每个菜单都有多个菜单项;单个菜单项可以负责子菜单;所有这些菜单都通过 [NSApp mainMenu] 锚定到UI。

Every menu owns multiple menu items; a single menu item can be responsible for a submenu; and all these menus are anchored to the UI by [NSApp mainMenu].

这篇关于使用NSMenuItems在其中创建NSMenu,以编程方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆