0

次のような外部クラスでメソッドをセットアップしようとしています。

myClass.m

- (void)drawSomeStuffInContext:(CGContextRef)context atStartingPoint:(CGPoint *)coords
{
    //draw some stuff (ignoring coords for now)
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextSetLineWidth(context, 1.0f);
    CGContextMoveToPoint(context, 10, 10); 
    CGContextAddLineToPoint(context, 100, 50);
    CGContextStrokePath(context);
}

viewController.m

- (void)viewDidLoad
{
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGPoint startCoords = CGPointMake(100.0, 100.0);
    [[myClass alloc] drawSomeStuffInContext:currentContext atStartingPoint:startCoords];
}

プロジェクトはビルドおよび実行されますが、ログに次のエラーが表示され、何も描画されません。

[...] <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0
[...] <Error>: CGContextSetLineWidth: invalid context 0x0
[...] <Error>: CGContextMoveToPoint: invalid context 0x0
[...] <Error>: CGContextAddLineToPoint: invalid context 0x0
[...] <Error>: CGContextDrawPath: invalid context 0x0

私は運がない同様の質問/例を求めてウェブを精査してきました。必要な別の/追加のパラメーターはありますか? 以外の場所から draw メソッドを呼び出す必要がありviewDidLoadますか? アドバイスをいただければ幸いです。

4

1 に答える 1

1

drawRect が呼び出されていない (私が知る限り) これは、ビューがまったく描画されていないことを意味します。[super initWithFrame:frame];drawSomeStuffInContext メソッドの where frame is a CGRect (明らかに) のようなものを呼び出す必要があります。

[[myClass alloc] initWithFrame:frame]; と呼ぶこともできます。viewController.m の viewDidLoad メソッドで開始点をグローバル変数に格納します。

于 2012-06-29T10:24:36.130 に答える