【问题标题】:NSWindowController not hiding its window at init?NSWindowController 没有在初始化时隐藏它的窗口?
【发布时间】:2011-05-20 11:54:21
【问题描述】:

我有一个 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

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

感谢任何帮助,谢谢:)

【问题讨论】:

    标签: objective-c nswindow nsdocument nswindowcontroller


    【解决方案1】:

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

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

    • 1 NSDocument 子类
    • 2个NSWindowControllers子类
    • 1 MainMenu.xib
    • 2个window.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];
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多