カスタム UIView サブクラスで DrawRect を呼び出すと、次のエラーが発生します。
CGContextFillRects: invalid context 0x0
UIView サブクラス「EVGameView」の DrawRect メソッドは、次のように実装されています。
- (void)drawRect:(CGRect)rect
{
CGRect rectangle = CGRectMake(0, 100, 320, 100);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextFillRect(context, rectangle);
}
そして、viewController 'viewDidLoadMethod' で EVGameView クラスを次のようにインスタンス化します。
- (void)viewDidLoad
{
srand(time(0));
[super viewDidLoad];
gameBoard = [[EVBoard alloc] init];
[gameBoard initBoard:10 : 10 : mainBoard];
gameView = [[EVGameView alloc] init]; // EVGameView instance
.
.
.
}
次のメソッドで drawRect メソッドを呼び出します。
- (void)drawStartPosition
{
CGRect rect;
rect.origin = startPos;
rect.size.width = 10;
rect.size.height = 10;
[gameView drawRect: rect];
}
UIGraphicsGetCurrentContext() が「nil」を返すため、コンテキストが設定されていないようです。人々が同じ drawRect メソッドを持っていて、それがうまく動作する例を youTube で見たことがあります。.xib ファイルに別のビューを設定し、それを gameView インスタンスにリンクしましたが、それでもうまくいきません。これについての助けは大歓迎です!
ありがとう-