NSOpenGLView 在 macOS 10.9 上删除了它的 NSOpenGLContext

NSOpenGLView drops its NSOpenGLContext on macOS 10.9

NSOpenGLView class 有一个 read/write 属性 openGLContext,在我阅读文档时,getter 永远不会 return无:

If the receiver has no associated context object, a new NSOpenGLContext object is created. The new object is initialized with the receiver’s pixel format information.

在 macOS 10.13 上,这似乎是正确的,但在 macOS 10.9 上,在我将视图添加到 window 后,它 return 为零。也就是说,如果我这样做:

NSOpenGLView* glView = [[NSOpenGLView alloc]
    initWithFrame: NSMakeRect( 0.0, 0.0, 100.0, 100.0 )
    pixelFormat: [NSOpenGLView defaultPixelFormat]];
NSLog(@"Pixel format %@, context %@", glView.pixelFormat, glView.openGLContext );

[self.host addSubview: glView];
NSLog(@"Pixel format %@, context %@", glView.pixelFormat, glView.openGLContext );

然后在第二个日志上,上下文为nil。有什么办法吗?制作我自己的 NSOpenGLContext 并分配它没有帮助。我已经验证主机视图是 window 的一部分并且 windowNumber 是正的。

显然,在 Mavericks 中,必须设置视图的上下文以及设置视图的上下文。像这样的东西有效:

NSOpenGLPixelFormat* pf = [NSOpenGLView defaultPixelFormat];

NSOpenGLView* glView = [[[NSOpenGLView alloc]
    initWithFrame: NSMakeRect( 0.0, 0.0, 100.0, 100.0 )
    pixelFormat: pf] autorelease];

NSOpenGLContext* glc = [[[NSOpenGLContext alloc]
    initWithFormat: glView.pixelFormat shareContext: nil] autorelease];

[self.host addSubview: glView];

glc.view = glView; // the key step I was missing
glView.openGLContext = glc;
NSLog(@"Pixel format %@, context %@", glView.pixelFormat, glView.openGLContext );