在IOS上添加Next / Done按钮到Number Pad?

时间:2013-12-20 07:26:09

标签: ios objective-c uikeyboard numpad

我需要在数字键盘上实现next / done按钮。

我正在使用以下代码。

但我在选择键盘视图时遇到异常。索引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)];
}

1 个答案:

答案 0 :(得分:1)

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

因为您的应用程序始终有one窗口。

更新:请参阅此post