0

我需要在数字键盘上实现下一个/完成按钮。

我正在使用下面的代码。

但是我在选择键盘视图时例外。索引 1 超出范围 [0...0]

这是我的代码;

//doneButton initialisation
doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
[doneButton addTarget:self action:@selector(textFieldShouldReturn:) forControlEvents:UIControlEventTouchUpInside];


//the exception i am taking
else if(textField == cardNumberField)
{

    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard;
    for(int i=0; i<[tempWindow.subviews count]; i++) {
        keyboard = [tempWindow.subviews objectAtIndex:i];
        // keyboard view found; add the custom button to it
        if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) {
            [keyboard addSubview:doneButton];
        }
    }

    [mainScrollView setContentSize:CGSizeMake(0, mainScrollView.contentSize.height+185)];

    [mainScrollView setContentOffset:CGPointMake(0, mainScrollView.frame.origin.y+185)];
}
4

1 回答 1

1

此行导致错误:UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];.

因为你的应用程序总是有one窗口。

更新:见这篇文章

于 2013-12-20T07:49:13.207 回答