2013-08-17 41 views
0

好的,我在這裏編寫了一個似乎無法運行的算法。基本上它會在多個頁面上繪製一個按鈕列表。它經過調整,以便iPhone 5和iPhone 4S以及更早版本的每頁上繪製4個按鈕。由於未捕獲的異常'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

一旦你有這個,你應該看到確切的哪一行代碼導致異常。

相關問題