这可能是一个简单的问题,但请耐心等待,我是非常新的,只是在尝试。MacOS,不是iOS,比方说我有两个独立的XIB文件(MainMenu.xib和AnotherWindow.xib)。我使用MainMenu.xib中的文件菜单来打开AnotherWindow.xib,并在它打开时禁用文件菜单。
- (IBAction)OpenAnotherWindow:(id)sender {
if (!anotherWindow) {
anotherWindow = [[AnotherWindow alloc] initWithWindowNibName:@"AnotherWindow"];
}
[anotherWindow showWindow:self];
[self.MenuItem setEnabled:NO];
在AnotherWindow.xib中,我想在它关闭时使用文件菜单重新启用。
- (void)windowWillClose:(NSNotification *)aNotification {
[self.MenuItem setEnabled:YES];
}
我的问题是我无法从第二个类中访问MenuItem,因为它是MainMenu.xib的一部分--所以我得到了错误信息。在AnotherWindow.xib中,在类型为[self.MenuItem setEnabled:YES]的对象上找不到属性。
所以,我想我的问题是:我如何才能访问一个像
@property (weak) IBOutlet NSMenuItem *MenuItem;
这是在我的MainMenu.xib中,来自AnotherWindow.xib。
与其手动启用和禁用菜单项,不如覆盖该功能。validateUserInterfaceItem
的班级中。OpenAnotherWindow
IBAction。
该 validateUserInterfaceItem
函数接收一个类型为 NSValidatedUserInterfaceItem
作为参数。检查项目的动作是否为 OpenAnotherWindow
. 如果是,检查是否 anotherWindow
是否打开。如果窗口打开了,返回false,将禁用菜单项。如果窗口没有打开,则返回true,这将启用菜单项。我的Objective-C很生疏,所以没有给你列出代码。