2013-08-17 2 views
0

Хорошо, поэтому я написал алгоритм здесь, который, кажется, не работает. В основном он рисует список кнопок на нескольких страницах. Он адаптирован таким образом, что на странице iPhone 5 будут отображаться 4 кнопки и 3 на iPhone 4S и ранее.Завершение приложения из-за неперехваченного исключения «NSInvalidArgumentException», причина: '- [__ NSCFArray length]: непризнанный селектор, отправленный экземпляру

Но по какой-то причине, в методе «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]; 
    } 
} 

Спасибо, ребята!

+0

Можете ли вы показать нам метод numOfPagesFromArray? – David

+0

Где именно находится "screenheight"? –

+0

Запустите его под инструментом Зомби. –

ответ

0

В Xcode перейдите к навигатору точек останова и добавьте контрольную точку исключения. Простое описание, как это сделать: in this SO answer

После этого вы должны увидеть, какая строка кода вызывает исключение.