0

With adapting my app to iOS7 I got the following error when an custom UIView is initialized: Assertion failed: (CGFloatIsValid(x) && CGFloatIsValid(y)),....

When the UIView is initialized the drawRect method is called and stops working due to missing datas handled later by another ViewController. The UIView is initialized when the storyboard scene containing the UIView is called.

What is the right way to make sure that the drawRect method is not called right after the initializing.

My UIView drawRect: method with the line where the error occurs:

- (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
        }
    }
}

另一项观察 - 为什么你的循环是硬编码的?为什么循环不是基于数组的长度?

于 2013-10-30T21:39:36.570 回答