0

アプリを iOS7 に適応させると、カスタム UIView が初期化されるときに次のエラーが発生しましたAssertion failed: (CGFloatIsValid(x) && CGFloatIsValid(y)),...

UIView が初期化されると、drawRect メソッドが呼び出され、後で別の ViewController によって処理されるデータが欠落しているため、動作が停止します。UIView を含むストーリーボード シーンが呼び出されると、UIView が初期化されます。

初期化直後に drawRect メソッドが呼び出されないようにする正しい方法は何ですか。

drawRect:エラーが発生した行を含む私の UIViewメソッド:

- (void)drawRect:(CGRect)dirtyRect{
for (NSInteger i=2; i<=101; i++) {
    height = fieldHeight * [[bellCurveArray objectAtIndex:i-1]doubleValue];
    CGContextAddLineToPoint(context, x1+t*(i-1), bounds.size.height-y1-height);
    //the height is causing the trouble due to the empty bellCurveArray in the early UIView stage
}
4

2 に答える 2

2

drawRect の呼び出しを避けるには、init メソッドでフレームを CGRectZero に設定します。

于 2013-10-30T20:23:42.200 に答える
2

あなたのコメントに基づいて、あなたのdrawRect:実装はまだ設定されていないデータをうまく処理していないようです。次のようなものが必要になる場合があります。

- (void)drawRect:(CGRect)dirtyRect{
    if (bellCurveArray.length) {
        for (NSInteger i=2; i<=101; i++) {
            height = fieldHeight * [[bellCurveArray objectAtIndex:i-1]doubleValue];
            CGContextAddLineToPoint(context, x1+t*(i-1), bounds.size.height-y1-height);
            //the height is causing the trouble due to the empty bellCurveArray in the early UIView stage
        }
    }
}

もう1つの観察-ループがハードコードされているのはなぜですか? ループが配列の長さに基づいていないのはなぜですか?

于 2013-10-30T21:39:36.570 に答える