可可:如何重新打开关闭窗口?

时间:2015-07-12 13:22:36

标签: objective-c xcode cocoa delegates nsview

我没有nib \ xib文件制作程序。这是简单的NSOpengGl窗口。它工作正常,但当我关闭窗口(红色按钮)并想再次打开它(通过单击任务栏图标) - 它不起作用。 applicationShouldHandleReopen - 不调用。 这是我的代码:

   @interface OGLView: NSOpenGLView <NSApplicationDelegate,NSWindowDelegate>
   @end

   @implementation OGLView
   -(BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
 {
  printf("bingo");//does not called  
 return YES;
 }

@end

和init:

....
view = [[OGLView alloc] initWithFrame:glRect pixelFormat:fmt];
[[view openGLContext] makeCurrentContext];

win = [ [NSWindow alloc]
      initWithContentRect: e
       styleMask:
      NSTitledWindowMask
     | NSClosableWindowMask
       |NSMiniaturizableWindowMask
      |  NSResizableWindowMask       
       backing: NSBackingStoreBuffered
       defer: YES ];

[win orderFrontRegardless];
[win setReleasedWhenClosed:NO];
[win setContentView:view];
[win makeFirstResponder:view];
[win setAcceptsMouseMovedEvents:YES];
[win setContentView:view];
[view setNeedsDisplay:YES];

[win setDelegate:view ];

[view display];  
[win makeKeyAndOrderFront: view];
...

我认为applicationShouldHandleReopen没有调用,因为它必须不是从NSView对象而是从委托调用。但我正在使用

[win setDelegate:view ];

如何调用此委托方法? 谢谢!

1 个答案:

答案 0 :(得分:2)

在应用程序&#34; applicationShouldHandleReopen&#34;方法使你的窗口顺序

 -(BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
 {
    [win makeKeyAndOrderFront:self];
    return YES;
 }

你应该打电话给&#34; applicationShouldHandleReopen&#34;应用委托中的方法不在您的视图中。