2011-11-13 42 views
0

我前幾天發佈了一個問題,但我沒有包含足夠的代碼,而我還有一個問題。我不確定我是否應該使用[[UIApplication sharedApplication] windows],但是由於修復程序,我建議在前一天數組中沒有正確數量的值。Obj-C,'NSRangeException',原因:' - [NSMutableArray objectAtIndex:]:index 2 beyond bounds [0 .. 1]'

有人可以告訴/告訴我我哪裏出錯了,如果它的正確使用[[UIApplication sharedApplication] windows]

- (void) addDoneToKeyboard { 

    doneButton.hidden = NO; 

    //Add a button to the top, above all windows 
    NSArray *allWindows = [[UIApplication sharedApplication] windows]; 
    NSUInteger topWindowIndex = [allWindows count];// fix - 1; 
    UIWindow *topWindow = [allWindows objectAtIndex:topWindowIndex]; //SIGABRT 

終止應用程序由於未捕獲的異常 'NSRangeException',原因: ' - [NSMutableArray的objectAtIndex:]:索引2超出範圍[0..1]'

// check if top window is of keypad or else 
    NSString *topViewClassName = [NSString stringWithFormat:@"%@", 
      [topWindow class]]; 
    while (![topViewClassName isEqualToString:@"UITextEffectsWindow"]) { 
     --topWindowIndex; 

     if(topWindowIndex < 1) // fix 0 
      break; 

     topWindow = [allWindows objectAtIndex:topWindowIndex]; 
     topViewClassName = [NSString stringWithFormat:@"%@", [topWindow class]]; 
    } 

//

if(topWindowIndex < 1) { // fix 0 
     topWindowIndex = [allWindows count] - 1; 
     topWindow = [allWindows objectAtIndex:topWindowIndex]; 
    } 

    if (doneButton.superview) 
     [doneButton removeFromSuperview]; 

    [topWindow addSubview:doneButton]; 

    if (!doneButtonShownRecently) { 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationBeginsFromCurrentState:YES]; 
     [UIView setAnimationDuration:SLIDE_IN_ANIMATION_DURATION]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
     doneButton.frame = CGRectMake(0, 480-53, 
         doneButton.frame.size.width, 53); 
     [UIView commitAnimations]; 
    } else { 
     doneButton.frame = CGRectMake(0, 427, 
         doneButton.frame.size.width, 53); 
    } 

    doneButtonShown = YES; 
} 

回答

1

在這裏你的代碼註釋似乎已經告訴你需要做什麼:

NSUInteger topWindowIndex = [allWindows count] - 1; 
UIWindow *topWindow = [allWindows objectAtIndex:topWindowIndex]; 

計數從1開始,而數組下標0開始,你的兩個數組索引值是0(你的第一個窗口)和1(對於你的第二個窗口),但您試圖訪問索引值2

編輯:注意下面的代碼是現在在上面完全是多餘的:

if(topWindowIndex < 1) { // fix 0 
topWindowIndex = [allWindows count] - 1; 
topWindow = [allWindows objectAtIndex:topWindowIndex]; 
} 
+0

謝謝您的回答,如果我這樣做,我得到一個警告'警告:語義問題:無符號表達<0的比較總是FALSE'上'如果(topWindowIndex <0 )' – Jules

+0

topWindowIndex是一個NSUInteger,一個* unsigned *整數,因此,你不能比較它小於0(負),因爲它不能成爲負數。 – Fuggly

+0

你已經將topWindowIndex定義爲一個無符號整數,這意味着它永遠不會小於0,這是正確的,因爲你永遠不會有一個窗口在索引-1或-2。所以這個警告不是問題 - 問題是你想用尋找這樣的索引值的代碼來實現什麼?還請注意,代碼不會出現在上面。 –

1

這個代碼,你在這裏也有錯:

// check if top window is of keypad or else 
NSString *topViewClassName = [NSString stringWithFormat:@"%@", 
     [topWindow class]]; 
while (![topViewClassName isEqualToString:@"UITextEffectsWindow"]) { 
    --topWindowIndex; 

    if(topWindowIndex < 1) // fix 0 
     break; 

    topWindow = [allWindows objectAtIndex:topWindowIndex]; 
    topViewClassName = [NSString stringWithFormat:@"%@", [topWindow class]]; 
} 

你真正想要的是:

// check if top window is of keypad or else 
BOOL foundTextEffectsWindow = NO; 
while (topWindowIndex >= 0) { 
    topWindow = [allWindows objectAtIndex:topWindowIndex]; 
    if ([topWindow isKindOfClass:[UITextEffectsWindow class]]) { 
     foundTextEffectsWindow = YES; 
     break; 
    } 

    topWindowIndex--; 
} 
if (foundTextEffectsWindow) { 
    // do stuff with the window 
} 
相關問題