0

私が作成しているボタンの配列があります。ただし、setCenter を呼び出すとコードがクラッシュします。

buttonsArray = [NSMutableArray new];
    for (int i = 0; i < 7; i++) {
        [buttonsArray addObject:[UIButton buttonWithType:UIButtonTypeCustom]];
        UIButton *tempButton = [buttonsArray objectAtIndex:i];
        [tempButton setFrameWidth:300.0 andHeight:50.0];
        [tempButton addBlackBorderWidth:1.0];
        [tempButton roundCornersBy:10.0];
        [tempButton setBackgroundColor:[UIColor blackColor]];
        [tempButton setColorToGradientFromColor:[UIColor grayColor] toColor:[UIColor blackColor]];
        [tempButton setButtonTextColorForNormalState:[UIColor whiteColor] highlightedState:[UIColor redColor]];
        [tempButton setCenter:CGPointMake([self screenUsableWidth] / 2.0, ([self screenUsableHeight] - 2.0 * MAIN_MENU_BUTTON_TO_SIDE_DISTANCE) * i / ([buttonsArray count] - 1) + MAIN_MENU_BUTTON_TO_SIDE_DISTANCE)]; //crash is here
    }

エラーは*** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [384 nan]'

4

1 に答える 1

1

y座標計算の一部はで割ること([buttonsArray count] - 1)です。

ループの最初の時点では、これは 0 に相当します。一般に、0 で割ることは悪いことと考えられています。

ゼロ除算を避けるために、その計算を変更する必要があります。おそらく、現在の配列数ではなく、実際のループ数に変更してください。

于 2013-04-13T23:25:54.703 に答える