1

2 つの異なる xib に対応する 2 つの NSWindowControllers を持つ NSDocument サブクラスがあります。

Document-Based Application Guide に従って、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];        
}

問題は、2 番目のウィンドウ コントローラー 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

さて、もう一度私自身の質問に答えますが、今回は前向きな発言をします。私が間違っていたことは、デフォルトのドキュメントアプリケーションテンプレートのドキュメントベースのアーキテクチャの隠された(私にとっては)影響と関係があると思います。

私は別のアプローチを試しました。「ドキュメントベースのアプリケーション」にフラグを立てずにアプリケーションを最初から作成し、次のものを提供します。

  • 1NSDocumentサブクラス
  • 2つのNSWindowControllersサブクラス
  • 1 MainMenu.xib
  • 2 window.xib

MyDocumentコードでNSWindowControllerサブクラスのインスタンス化を強制しました。

また、MenuItemのIBActionをMyDocumentに配置し、MyDocumentオブジェクトをMainMenu.xibのMenuItemにバインドしました。

今回は、非表示のウィンドウから始めてウィンドウを非表示/表示したり、メニュー項目を自由に自動的に有効にしたりすることができました。

将来これと戦わなければならないかもしれない私のような初心者のために、ここにコードが続きます。

//  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 に答える