问题描述
我有一个包含窗口的nib(winA.xib)。我的应用程序委托包含一个名为WinAController的NSWindowController子类。
WinAController有一个属性(NSMenu * mainMenu),我要指向MainMenu。我设置它之后我实例化WinAController与这段代码:
WinAController = [[WinAController alloc] initWithWindowNibName:@WinA ;
WinAController.mainMenu = [NSApp mainMenu];
我在MainMenu的Window顶层菜单项下有一个菜单项,调用[ WinAController showWindow]方法并显示WinA。我想根据WinA是否可见来切换此菜单项的开/关状态。 WinAController也有另一个属性(NSMenuItem * myMenuItem)。
如何获得对窗口顶级菜单项的子菜单的引用。我想获得的子菜单项的标题是命令。我试过这个:
if(mainMenu!= nil){
myMenuItem = [mainMenu itemAtIndex:[mainMenu indexOfItemWithTitle :@命令]];
}
但它似乎不起作用。
我错了什么?
感谢,
:我现在把WinAController放在mainMenu.xib中。我设置WinA(在winA.xib)文件的所有者是WinAController类,但我不知道如何将WinAController的窗口IBOutlet连接到WinA,因为他们在不同的nibs!
哦,没有什么冒险,没有获得。你想做什么,当然是以下:
@interface MyApplicationDelegate:NSObject {
IBOutlet NSMenuItem * winAMenuItem;
}
@property(assign)IBOutlet NSMenuItem * winAMenuItem;
@end
然后您可以通过 [[NSApp delegate] winAMenuItem];
I have a nib (winA.xib) that contains a window. My app delegate contains an NSWindowController subclass called WinAController.
WinAController has a property (NSMenu *mainMenu) that I want to point to the MainMenu. I have set it after I instantiate WinAController with this code:
WinAController = [[WinAController alloc] initWithWindowNibName:@"WinA"];
WinAController.mainMenu = [NSApp mainMenu];
I have a menu item underneath the "Window" top-level menu item on MainMenu that invokes the [WinAController showWindow] method and displays WinA. I want to toggle the on/off state of this menu item depending on whether WinA is visible or not. WinAController also has another property (NSMenuItem *myMenuItem).
How can I get a reference to a sub menu of the "Window" top-level menu item. The title of sub menu item I want to get is "Command". I have tried this:
if (mainMenu != nil) {
myMenuItem = [mainMenu itemAtIndex:[mainMenu indexOfItemWithTitle:@"Command"]];
}
But it doesn't seem to work.
Where am I going wrong?
Thanks,
Edit: I have now placed WinAController in mainMenu.xib. I have set WinA's (in winA.xib) File's Owner to be of class WinAController but I can't figure out how to hook up WinAController's window IBOutlet to WinA as they are in different nibs!
I thought I told you to put the Window Controllers in MainMenu.xib?
Oh well, nothing ventured, nothing gained. What you want to do, of course, is the following:
@interface MyApplicationDelegate : NSObject {
IBOutlet NSMenuItem *winAMenuItem;
}
@property(assign) IBOutlet NSMenuItem *winAMenuItem;
@end
Then you can access this through [[NSApp delegate] winAMenuItem];
这篇关于从MainMenu获取特定菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!