I posted a question the other day, but I didn’t included enough code and I’m having a further problem. I’m not sure if I should be using [[UIApplication sharedApplication] windows], but since the fix I was advise to make the other day the array doesn’t have the correct amount of values.
Can someone advise / show me where I’m going wrong and if its correct to use [[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
Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘ -[NSMutableArray objectAtIndex:]: index 2 beyond bounds [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;
}
Your code comments here appear to already tell you what you need to do:
Counting starts with 1, while array indexes start at 0—your two array index values are 0 (for your first window) and 1 (for your second window) but you were trying to access index value 2.
Edit: Note the following code is now completely redundant in the above: