Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios drawRect成员函数内部的上下文错误无效_Ios_Objective C_Cgcontext - Fatal编程技术网

Ios drawRect成员函数内部的上下文错误无效

Ios drawRect成员函数内部的上下文错误无效,ios,objective-c,cgcontext,Ios,Objective C,Cgcontext,在XCode 4.6.3中运行iOS程序时,我不断收到一长串错误,如下所示: Jul 20 13:24:40 ps2xipas3qfe Chess[277] <Error>: CGContextSetRGBFillColor: invalid context 0x0 Jul 20 13:24:40 ps2xipas3qfe Chess[277] <Error>: CGContextFillRects: invalid context 0x0 这是我在ChessBoard

在XCode 4.6.3中运行iOS程序时,我不断收到一长串错误,如下所示:

Jul 20 13:24:40 ps2xipas3qfe Chess[277] <Error>: CGContextSetRGBFillColor: invalid context 0x0
Jul 20 13:24:40 ps2xipas3qfe Chess[277] <Error>: CGContextFillRects: invalid context 0x0
这是我在ChessBoard.m中的一个函数:

- (void)viewDidLoad
{
    [super viewDidLoad];
    ChessBoard* TheBoard = [ChessBoard new];
    [self.view addSubview: TheBoard];
    // [TheBoard setNeedsDisplayInRect: CGRectMake(0,0,400,400)];
    [TheBoard setNeedsDisplay];
}
- (void)drawRect: (CGRect)rect {
    [super drawRect:rect];
    UILabel* HelloWorld = [UILabel new];
    HelloWorld.text = @"Hello, World!";
    [HelloWorld sizeToFit];
    HelloWorld.frame = CGRectMake(1,1,100,20);
    [self addSubview:HelloWorld];
    for(int i=0; i<8; i++) {
        for(int j=0; j<8; j++) {
            CGRect TheRect = CGRectMake(i*30+30,j*30+30,30,30);
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextFillRect(context,TheRect);
            if(i%2 == j%2) {
                CGContextSetRGBFillColor(context,1.0,0.0,0.0,1.0);
            }
            else {
                CGContextSetRGBFillColor(context,0.0,0.0,0.0,1.0);
            }
        }
    }
}
-(void)drawRect:(CGRect)rect{
[超级drawRect:rect];
UILabel*HelloWorld=[UILabel new];
HelloWorld.text=@“你好,世界!”;
[HelloWorld sizeToFit];
HelloWorld.frame=CGRectMake(1,1100,20);
[自添加子视图:HelloWorld];

对于(int i=0;i您的代码非常接近!但是,您用于实例化棋盘实例的代码是不正确的。UIView的文档指出,您必须使用initWithFrame:initializer方法,而不是使用“new”(无论如何,在Objective-C中应始终避免使用“new”)

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect boardFrame = CGRectMake(0, 0, 240, 240);
    ChessBoard *theBoard = [[ChessBoard alloc] initWithFrame:boardFrame];
    [self.view addSubview:theBoard];
}

如果你尝试,你应该看到你的绘图代码工作。

是的,我显式地调用了
drawRect
。我没有意识到我不应该这样做。永远不要调用
drawRect:
。它甚至在
UIView drawRect:
的文档中告诉你这一点。如果你想强制视图再次绘制,请调用
setNeedsDisplay
在视图上。查看Josh引用的副本。感谢您的帮助。非常有效。
- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect boardFrame = CGRectMake(0, 0, 240, 240);
    ChessBoard *theBoard = [[ChessBoard alloc] initWithFrame:boardFrame];
    [self.view addSubview:theBoard];
}