我前幾天發佈了一個問題,但我沒有包含足夠的代碼,而我還有一個問題。我不確定我是否應該使用[[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;
}
謝謝您的回答,如果我這樣做,我得到一個警告'警告:語義問題:無符號表達<0的比較總是FALSE'上'如果(topWindowIndex <0 )' – Jules
topWindowIndex是一個NSUInteger,一個* unsigned *整數,因此,你不能比較它小於0(負),因爲它不能成爲負數。 – Fuggly
你已經將topWindowIndex定義爲一個無符號整數,這意味着它永遠不會小於0,這是正確的,因爲你永遠不會有一個窗口在索引-1或-2。所以這個警告不是問題 - 問題是你想用尋找這樣的索引值的代碼來實現什麼?還請注意,代碼不會出現在上面。 –