2012-11-18 23 views
2

我使用此代码在视图中绘制一些矩形。 这是代码:为什么我在基于文档的应用程序中收到无效的上下文0x0错误?

CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort]; 
CGContextSetRGBFillColor (myContext, 1, 0, 0, 1); 
CGContextFillRect (myContext, CGRectMake (0, 0, 200, 100)); 
CGContextSetRGBFillColor (myContext, 0, 0, 1, .5); 
CGContextFillRect (myContext, CGRectMake (0, 0, 100, 200)); 

当我在一个简单的AppDelegate使用代码,一切都还好。但是,当我(在Document.m)任何基于文档的应用程序中使用它,我收到此错误:

<Error>: CGContextSetRGBFillColor: invalid context 0x0 
<Error>: CGContextFillRects: invalid context 0x0 
<Error>: CGContextSetRGBFillColor: invalid context 0x0 
<Error>: CGContextFillRects: invalid context 0x0 

我缺少什么?

回答

2

我想我是个白痴!当前上下文始终设置为主窗口。我可以通过继承一个视图并使用此代码来对其进行管理:

- (void)drawRect:(NSRect)dirtyRect { 
    [super drawRect:dirtyRect]; 
} 
相关问题