0

好的,所以我在这里编写了一个似乎不起作用的算法。基本上,它会在多个页面上绘制一个按钮列表。它经过调整,以便在 iPhone 5 上每页绘制 4 个按钮,在 iPhone 4S 和更早版本上绘制 3 个按钮。

但由于某种原因,在“setFrame”方法中,我收到了这个错误:

由于未捕获的异常而终止应用程序:

'NSInvalidArgumentException', reason: '-[__NSCFArray length]: unrecognized selector sent to instance

奇怪的是,算法中没有任何地方,或者应用程序中的任何其他地方都没有length];调用该方法。

另请注意,我不知道何时使用此方法,并且我也知道NSArrays没有任何方法length

这是算法:

// creates the chapters
+(void)createChapterInScrollView:(UIScrollView *)scrollview withArray:(NSArray *)array withTarget:(id)target andMethod:(SEL)method
{
    // sets up some initial variable
    int x = 20;
    int y = 40;

    // fills an array with the y values
    int yValues;
    NSMutableArray *yContainer = [[NSMutableArray alloc] init];
    if (screenHeight == 568) {
        yValues = 4;
    } else {
        yValues = 3;
    }
    for (yValues = yValues; yValues > 0; yValues = yValues - 1) {
        [yContainer addObject:[NSString stringWithFormat:@"%i", y]];
        y = y + 70;
    }

    // creates the buttons using the number of pages
    int numOfpages = [self numOfPagesFromArray:array];
    int numofPagesLeft = [self numOfPagesFromArray:array];
    int numOfButtonsLeft = [array count];
    int currentButton = 0;

    if (numofPagesLeft == 0) {

    } else {
        for (numofPagesLeft = numofPagesLeft; numofPagesLeft >= 0; numofPagesLeft = numofPagesLeft - 1) {
            for (id object in yContainer) {
                if (numOfButtonsLeft > 0) {
                    UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
                    [newButton setTitle:[array objectAtIndex:currentButton] forState:UIControlStateNormal];
                    [newButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
                    [newButton setFrame:CGRectMake(x, [object floatValue], 280, 50)];
                    [newButton setTag:(currentButton+1)];
                    [newButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
                    [newButton.titleLabel setTextAlignment:NSTextAlignmentLeft];
                    [newButton addTarget:target action:method forControlEvents:UIControlEventTouchUpInside];
                    [scrollview addSubview:newButton];
                    numOfButtonsLeft = numOfButtonsLeft - 1;
                    currentButton ++;
                }
            }
            x = x + 320;
        }
    }

    // checks if any buttons were actually created
    if (numOfpages == 0) {
        // tells the user that no content has been downloaded
        UILabel *errorLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 216)];
        [errorLabel setBackgroundColor:[UIColor clearColor]];
        [errorLabel setTextColor:[UIColor whiteColor]];
        [errorLabel setNumberOfLines:10];
        [errorLabel setFont:[UIFont systemFontOfSize:17]];
        [errorLabel setTextAlignment:NSTextAlignmentCenter];
        [errorLabel setText:@"Oops!\n\nLooks like no readable content has been downloaded!\n\nThe content will be automatically downloaded right now!"];
        [scrollview addSubview:errorLabel];
    }
}

多谢你们!

4

1 回答 1

0
于 2013-08-17T23:04:51.537 回答