0

前几天我发布了一个问题,但我没有包含足够的代码,而且我遇到了另一个问题。我不确定我是否应该使用[[UIApplication sharedApplication] windows],但自从修复以来,我被建议在前几天使数组没有正确数量的值。

有人可以建议/告诉我我哪里出错了,是否正确使用[[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:]: 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;
}
4

2 回答 2

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];
}
于 2011-11-13T10:27:26.873 回答
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
}
于 2011-11-13T11:05:45.237 回答