【问题标题】:Closed NSWindow shows up when NSSavePanel is presented (osx cocoa)出现 NSSavePanel 时显示已关闭的 NSWindow (osx cocoa)
【发布时间】:2015-07-15 15:20:25
【问题描述】:

我编写了一个带有启动画面的 OSX 故事板应用程序。它是一个带有 NSWindowController 的 NSWindow,没有子类化,它有一个 SplashViewController,它是 NSViewController 的子类。

这是SplashViewController的代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    _timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timeOut) userInfo:nil repeats:NO];
}

- (void)timeOut
{
    [self performSegueWithIdentifier:@"main" sender:self];
}

- (void)prepareForSegue:(NSStoryboardSegue *)segue sender:(id)sender
{
    [_timer invalidate];
     _timer = nil;

    [self.view.window.windowController close];
}

这会按预期关闭窗口/窗口控制器。

segue 有 show 类型并呈现另一个 NSWindowController,它具有 MainViewController,NSViewController 的子类。当 MainViewController 打开保存对话框时:

NSArray *a = [[NSApplication sharedApplication] windows];
NSLog(@"%ld", a.count);

NSSavePanel *savePanel = [NSSavePanel savePanel];
[savePanel setAllowedFileTypes:@[@"pdf"]];
[savePanel setDirectoryURL:[NSURL URLWithString:NSHomeDirectory()]];
[savePanel beginSheetModalForWindow:self.view.window completionHandler:nil];

出现一个旧的启动窗口(并打印“1”)。什么?我的意思是为什么它会出现? NSSavePanel 和它有什么关系?

【问题讨论】:

    标签: objective-c macos cocoa nswindow nswindowcontroller


    【解决方案1】:

    感谢this answer(虽然部分无关)我设法让它工作。

    使用故事板时似乎有几个错误(我使用的是 XCode 6.3.1)。为了修复它们,您需要:

    • 选择您的初始窗口控制器并取消选中“是初始控制器”(即没有为情节提要设置初始控制器)。

    • 您的主窗口控制器应该被子类化,即使子类为空。

    • 将主窗口控制器的 Storyboard ID 设置为 Main(如果您省略上一步,Storyboard ID 在您离开 Storyboard 后立即被擦除,第一个错误)。

    • 在您的 AppDelegate.m 中添加以下代码:

      @property (strong, nonatomic) NSWindowController *mainWindowController;
      
      - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
      {
          [NSApp activateIgnoringOtherApps:YES];
      
          NSStoryboard *storyboard = [NSStoryboard storyboardWithName:@"Main" bundle:nil];
          NSWindowController *main = [storyboard instantiateControllerWithIdentifier:@"Main"];
          [main showWindow:self];
          [main.window makeKeyAndOrderFront:self];
      
          // Important! Should be strong to retain instance. Second bug. 
          // If you don't do that, your window will open and close right away.
          _mainWindowController = main; 
      }
      

    【讨论】:

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