1

我有一个 NSDocument 子类,其中有两个 NSWindowController 对应于 2 个不同的 xib。

遵循基于文档的应用程序指南,我在 document.m 实现中添加了以下内容

- (void)makeWindowControllers 
{
    NSLog(@"in MakeWindowControllers");

    MainWindowController *mainWindowController = [[MainWindowController alloc] init];
    [mainWindowController autorelease];
    [self addWindowController:mainWindowController];

    csvWindowController = [[CSVWindowController alloc] init];
    [csvWindowController autorelease];
    [self addWindowController:csvWindowController];        
}

问题是我希望第二个窗口控制器 csvWindowController 最初隐藏它的窗口,稍后我将显示相同的窗口实例。为此,我写了:

@implementation CSVWindowController


- (id) init {

    if ( ! (self = [super initWithWindowNibName:@"CSVWindow"]) ) {

        NSLog(@"CSVWindowController init failed");
        return nil;
    }

    window = [self window];
    NSLog(@"CSVWindowController init");

    [window orderOut:nil]; // to hide it
    NSLog(@"CSVWindowController hiding the window");

    return self;
}

但是窗口在那里,出现了。

请不要我没有标记 VisibleAtLaunch,该控制台正在正确显示我的消息,即使我更改:

        [window orderOut:nil]; // to hide it
to 
        [window orderOut:self]; // to hide it

结果是一样的,窗口出现了。

任何帮助表示赞赏,谢谢:)

4

1 回答 1

1

好的,我再次回答我自己的问题,但这次是正面评价。我认为我做错的事情与隐藏的 - 对我来说 - 默认文档应用程序模板的基于文档的体系结构的含义有关。

我尝试了一种不同的方法,从头开始创建一个应用程序,而不是标记“基于文档的应用程序”并为其提供:

  • 1 NSDocument 子类
  • 2 NSWindowControllers 子类
  • 1 主菜单.xib
  • 2 窗口.xib

我已经在 MyDocument 代码中强制实例化 NSWindowController 子类。

我还将 MenuItems 的 IBActions 放在 MyDocument 中,并将 MyDocument 对象绑定到 MainMenu.xib 中的 MenuItems。

这次我可以做任何事情,隐藏/显示从一个隐藏的窗口开始的窗口,随意自动启用菜单项。

下面是代码,适用于像我这样将来可能不得不与之抗争的新手。

//  MyDocument.h
#import <Cocoa/Cocoa.h>
#import "testWindowController.h"
#import "test2WindowController.h"

@interface MyDocument : NSDocument {
    testWindowController *test;
    test2WindowController *test2;

}

- (IBAction)showWindow1:(id)pId;
- (IBAction)showWindow2:(id)pId;
- (IBAction)hideWindow1:(id)pId;
- (IBAction)hideWindow2:(id)pId;

@end


//  MyDocument.m
#import "MyDocument.h"
#import "testWindowController.h"
#import "test2WindowController.h"

@implementation MyDocument

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
        NSLog(@"MyDocument init...");
        [self makeWindowControllers];
    }

    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)makeWindowControllers 
{

    test = [[testWindowController alloc] init];
    test2 = [[test2WindowController alloc] init];  

    [self addWindowController:test];
    [self addWindowController:test2];

    // start hiding the first window
    [[test window] orderOut:self];
}

- (IBAction)hideWindow1:(id)pId
{
    NSLog(@"hideWindow1");
    [[test window] orderOut:self];
}

- (IBAction)showWindow1:(id)pId
{
    NSLog(@"showWindow1");
    [test showWindow:self];
    [[test window] makeKeyAndOrderFront:nil]; // to show it
}

- (IBAction)hideWindow2:(id)pId
{
    NSLog(@"hideWindow2");
    [[test2 window] orderOut:self];
}

- (IBAction)showWindow2:(id)pId
{
    NSLog(@"showWindow2");
    [test2 showWindow:self];
    [[test2 window] makeKeyAndOrderFront:nil]; // to show it
}


 -(BOOL)validateMenuItem:(NSMenuItem *)menuItem {

     NSLog(@"in validateMenuItem for item: %@", [menuItem title]);

     if ([[menuItem title] isEqualToString:@"Show Window"] 
         && [[test window] isVisible]){
         return NO;
     }

     if ([[menuItem title] isEqualToString:@"Hide Window"] 
         && ![[test window] isVisible]){
         return NO;
     }

     if ([[menuItem title] isEqualToString:@"Show Window2"] 
         && [[test2 window] isVisible]){
         return NO;
     }

     if ([[menuItem title] isEqualToString:@"Hide Window2"] 
         && ![[test2 window] isVisible]){
         return NO;
     }
     return [super validateMenuItem:menuItem];
 }
于 2011-05-22T18:09:52.267 回答